Design Patterns
Jeszenszky, Péter
University of Debrecen, Faculty of Informatics
[Link]@[Link]
Kocsis, Gergely (English version)
University of Debrecen, Faculty of Informatics
[Link]@[Link]
Last modified: 06.04.2017
References
java-design-patterns: Design patterns
implemented in Java
[Link]
Creational patterns (GoF)
● Abstract Factory
● Builder
● Factory Method
● Object Pool
● Prototype
● Singleton
3
Abstract Factory (1)
Aim:
– It provides an interface to create a family of
objects that are connected or depend on
each other without defining the exact
classes.
4
Abstract Factory (2)
● Example code:
[Link]
/tree/master/abstract-factory
5
6
Abstract Factory (4)
● Known uses
– [Link]
[Link]
[Link]
– [Link]
[Link]
/datatype/[Link]
– [Link]
[Link]
/transform/[Link]
– [Link]
[Link] 7
l/stream/[Link]
Builder (1)
Aim: It makes the creation of complex
objects independent of their representation.
As a result with the same process we can
create different representations.
8
Builder (2)
● Example code:
[Link]
/tree/master/builder
9
10
Builder (4)
● Known uses:
– [Link]
[Link]
[Link]
– [Link]
der
[Link]
/format/[Link]
– [Link]
[Link]
[Link]
11
Factory method (1)
● Aim:
– It provides an interface to create an object
but it lets the subclasses to decide which
class is instantiated
12
Factory method (2)
● Example:
[Link]
s/tree/master/factory-method
13
Factory method (3)
14
Factory method (4)
● Known uses:
– [Link]
ry#newDocumentBuilder()
[Link]
ml/parsers/[Link]#newDocumen
tBuilder--
– [Link]#new
SAXParser()
[Link]
ml/parsers/[Link]#newSAXParser--
15
Object pool (1)
● Aim: Registers a set of initialized objects for the
service of the demands instead of erasing and
re-instantiating them
16
Object pool (2)
● Example code:
[Link]
/tree/master/object-pool
17
Object pool (3)
● Known uses:
– Pooling database connections (connection pooling)
● E.g.:
– Apache Commons DBCP
[Link]
– HikariCP [Link]
– Vibur DBCP [Link]
18
Prototype (1)
● Aim: By providing a prototype instance it
defines what type of objects are to be created.
The object are copies of this prototype
19
Prototype (2)
● Example code:
[Link]
/tree/master/prototype
20
21
Prototype (4)
● Known uses:
– [Link]
[Link]
/[Link]
22
Singleton (1)
Aim: It lets only one instance to be created from a
class. For this it provides a global acces point.
23
Singleton (2)
● Example code:
[Link]
/tree/master/singleton
24
Singleton (3)
● Known uses:
– [Link]
[Link]
/[Link]
– [Link]
[Link]
/chrono/[Link]
25
More creational patterns
● Factory Kit
[Link]
● Module [Link]
● Monostate
[Link]
● Multiton
[Link]
● Object Mother
[Link]
● Step Builder
[Link]
● Value Object
[Link]
● …
26
Multition (1)
● Aim: It lets only a given number of instances to
be created from a class. For them it provides a
global acces point.
27
Multition (2)
● Example code:
[Link]
/tree/master/multiton
28
Multition (3)
● Known uses:
– [Link]
[Link]
/[Link]
– [Link]
[Link]
h/[Link]
– [Link]
[Link]
e/[Link]
– [Link]
[Link] 29
chrono/[Link]
Structural patterns (GoF)
● Adapter
● Bridge
● Composite
● Decorator
● Facade
● Flyweight
● Proxy
30
Adapter (1)
● Aim:
– It converts the interface of a class to the interface
that can be used by other classes.
– By the use of it classes with incompatible intefaces
may work together
31
Adapter (2)
● Example code:
[Link]
/tree/master/adapter
32
Adapter (3)
● Known uses:
– [Link]
[Link]
[Link]
– [Link]
[Link]
[Link]
– [Link]
[Link]
tion/adapters/[Link]
– [Link]
[Link]
[Link]
– [Link]
[Link] 33
[Link]
Bridge (1)
Aim: It separates the abstract representation
from the implementation so that they can be
modified separately.
34
Bridge (2)
● Example code:
[Link]
/tree/master/bridge
35
36
Bridge (4)
● Known uses:
– JDBC [Link]
37
Composite (1)
● Aim:
– The objects are structured as a tree in order to
illustrate the part-whole relations
– As a result the separate objects and the composits
may be handled as well
38
Composite (2)
● Example code:
[Link]
s/tree/master/composite
39
Composite (3)
● Known uses:
– [Link]
[Link]
[Link]
– jsoup: Java HTML Parser [Link]
● [Link]
[Link]
40
Decorator (1)
● Aim:
– Adds more additional responsibilities to classes
dinamically.
– Can be used as an alternative of sub-classes.
41
Decorator (2)
● Example code:
[Link]
/tree/master/decorator
42
Decorator (3)
● Known uses:
– [Link]
[Link]
html
● Where the constructor gets InputStream objects, like
[Link].
– [Link]
[Link]
[Link]
● Where the constructor gets OutputStream objects, like
[Link].
– [Link]#unmodifiableXXX()
[Link]
.html 43
Facade (1)
● Aim:
– In a sub-system it provides a main interface that
gathers a set of other interfaces.
– By the use of it a higher level interface is provided
in order to make the use of the subsystem more
easy.
44
Facade (2)
● Example code:
[Link]
/tree/master/facade
45
46
Facade (4)
● Known uses:
– Simple Logging Facade for Java (SLF4J)
[Link]
47
Flyweight (1)
Aim:
– Supports the use of high mass granular objects.
48
Flyweight (2)
● Example:
[Link]
/tree/master/flyweight
49
Flyweight (3)
50
Flyweight (4)
● Known uses:
– [Link]#valueOf(byte b)
[Link]
api/java/lang/[Link]#valueOf-byte-
– [Link]#valueOf(char c)
[Link]
api/java/lang/[Link]#valueOf-c
har-
– [Link]#valueOf(int i)
[Link]
[Link]#valueOf-int-
51
Proxy (1)
Aim:
– A given object is replaced by another one that
overrides the access to the original object as
well.
52
Proxy (2)
● Example code:
[Link]
/tree/master/proxy
53
Proxy (3)
● Known uses:
– [Link]
[Link]
flect/[Link]
– Apache Commons Proxy
[Link]
/
54
More structural patterns
● Abstract Document
[Link]
e/master/abstract-document
● Module
[Link]
e/master/module
● Private Class Data
[Link]
/master/private-class-data
● Twin
[Link]
/master/twin 55
● …
Twin (1)
● Aim:
– Model multiple inheritance in languages where it is
not supported by default.
56
Twin (2)
● Example code:
[Link]
/tree/master/twin
57
Behavioral petterns (GoF)
● Chain of Responsibility
● Command
● Interpreter
● Iterator
● Mediator
● Memento
● Observer
● State
● Strategy
● Template Method
● Visitor
58
Chain of Responsibility (1)
● Aim:
– The aim is to avoid the connection of the sender of
the request to the receiver
– We do this by providing the right to handle the
request for more objects.
– The receiver objects may be chained. The object
goes in the chain while it reaches such an object
that can handle it.
59
Chain of Responsibility (2)
● Example code:
[Link]
/tree/master/chain
60
Chain of Responsibility (3)
61
Chain of Responsibility (4)
● Known uses:
– [Link]
[Link]
logging/[Link]
62
Command (1)
Aim:
– It closes queries to an object in order to make it
possible to give these queries as parameters to
other agents
63
Command (2)
● Example code:
[Link]
ns/tree/master/command
64
65
Command (4)
● Known uses:
– [Link]
[Link]
/[Link]
66
Interpreter (1)
● Aim: Represents the grammer of a given
language and provide an interpreter for it that
understands the sentences of that language.
67
Interpreter (2)
● Example code:
[Link]
tree/master/interpreter
68
Interpreter (3)
69
Interpreter (4)
● Known uses:
– [Link]
[Link]
[Link]
– [Link]
[Link]
e/format/[Link]
– [Link]
[Link]
egex/[Link]
70
Iterator (1)
Aim:
– It provides a sequential access to elements of
complex objects without showing the representation
of the objects
71
Iterator (2)
● Example code:
[Link]
tree/master/iterator
72
Iterator (3)
73
Iterator (4)
● Known uses:
– [Link]
[Link]
api/java/util/[Link]
– [Link]
[Link]
[Link]
74
Mediator (1)
● Aim:
– The aim is to provide an object that controls the the
cooperation of a set of objects
– By this mmethod we create a weak connection
where the objects do not refer to each other and the
connectins between them may be modified
independently of each other
75
Mediator (2)
● Example code:
[Link]
/tree/master/mediator
76
Mediator (3)
77
Mediator (4)
● Known uses:
– [Link]
[Link]
[Link]
78
Memento (1)
● Aim:
– Save the show the inner state of an object without
breaking enclosure so that later the object can be
loaded back to this previous state
79
Memento (2)
● Example code:
[Link]
ns/tree/master/memento
80
81
Memento (4)
● Known uses:
– [Link]
[Link]
[Link]
● See getTime() and setTime(long time) methods.
82
Observer (1)
● Aim:
– Provides a connection between objects so that if an
object changes all other objects depending on it are
informed about that
83
Observer (2)
● Example code:
[Link]
/tree/master/observer
84
Observer (3)
● Known uses:
– [Link]
[Link]
api/java/util/[Link]
– [Link]
[Link]
[Link]
[Link]
[Link]
[Link]
85
State (1)
● Aim:
– It lets the object to change its behavior by changing
its inner state.
– Appearingly the object changes its class.
86
State (2)
● Example code:
[Link]
/tree/master/state
87
State (3)
88
Strategy (1)
● Aim:
– Defines an algorithm family in which the algorithms
are collected to separate units making it able to
replece them by each other
– As a result the algorithm can be modified
independently of the client
89
Strategy (2)
● Example code:
[Link]
/tree/master/strategy
90
Strategy (3)
● Known uses:
– [Link]
[Link]
[Link]
● See binarySearch(), max(), min() and sort()
methods.
● Related interfaces:
– [Link]
[Link]
html
– [Link]
[Link]
tml
91
Template method (1)
● Aim
– It provides the skeleton of the algorithm of a given
method while letting the subclasses the implement
the steps.
– As a result the subclasses may override given steps
of the algorithm without modifying the structure of it
92
Template method (2)
● Example code:
[Link]
s/tree/master/template-method
93
Template method (3)
94
Template method (4)
● Known uses:
– [Link]
[Link]
ml
– [Link]
[Link]
tml
– [Link]
[Link]
ml
– [Link]
[Link]
tml
– [Link]
[Link]
[Link]
Visitor (1)
● Aim:
– Represents an operation that can be executed on
the elements of an object structure
– By the use of this pattern we can define a new
operation without the need of changing the class of
the affected elements
96
Visitor (2)
● Example code:
[Link]
tree/master/visitor
97
98
Visitor (4)
● Known uses:
– [Link]
[Link]
e/[Link]
– [Link]
[Link]
ng/model/element/[Link]
[Link]
r
[Link]
g/model/element/[Link]
99
More behavioral patterns
● Null Object
[Link]
tree/master/null-object
● Servant
[Link]
/tree/master/servant
● Specification
[Link]
/tree/master/specification
● …
100
Null object (1)
● Aim:
– Provide an alternative of the null reference to
indicate the absence of an object by the use of an
object that implements the required interface by
providing only empty method bodies for the abstract
methods.
101
Null object (2)
● Example code:
[Link]
tree/master/null-object
102