5. 3
DESIGN PATTERNS
In software engineering, a design pattern is a general
repeatable solution to a commonly occurring problem in
software design. A design pattern isn't a nished design that
can be transformed directly into code. It is a description or
template for how to solve a problem that can be used in many
different situations.
14. 7 . 2
7 . 3
Java Optimisation : Bill Pugh Singleton Implementation
public class BillPughSingleton {
private BillPughSingleton(){}
private static class SingletonHelper{
private static final BillPughSingleton INSTANCE
= new BillPughSingleton();
}
public static BillPughSingleton getInstance(){
return SingletonHelper.INSTANCE;
}
}
15. 8 . 1
De ne an interface for creating an object but let subclasses
decide which class to instanciate. The factory method lets a
class defer instantiation to subclasses.
FACTORY METHOD
16. 8 . 2
Basically, we create object without exposing the creation
logic and refer to newly created object using a common
interface.
20. 8 . 5
9 . 1
Provides an interface for creating families of related or
dependent objects without specifying their concrete classes.
This pattern provides an encapsulation mechanism to a group
of individual factories with a common theme.
ABSTRACT FACTORY
29. 10 . 4
11 . 1
Separate the construction of a complex object from its
representation so that the same construction processes can
create different representations.
BUILDER
38. 13 . 4
14 . 1
Use sharing to support large numbers of ne grained objects
ef ciently.
FLYWEIGHT
Flyweight pattern tries to reuse already existing similar kind
objects by storing them and creates new object only when no
matching object is found.
39. 14 . 2
Example : In a Role Playing Game, we have salesman Non
Playable Characters :
They don't move.
Event reactions are the same.
Only speci c caracteristics (prices, items, skin) vary.
48. 15 . 5
16 . 1
Provide a uni ed interface to a set of interfaces in a system.
Facade de nes a higher-level interface that makes the
subsystem easier to use. this pattern adds an interface to
existing system to hide its complexities.
FACADE
49. 16 . 2
Facade pattern hides the complexities of the system by
involving a single class which provides simpli ed methods
required by client and delegates calls to methods of existing
system classes.
This is the basic principle of an API...
50. 17 . 1
Attach additionnal responsibilities to an object dynamically.
Decorators provide a exible alternative to subclassing for
extending functionality.
DECORATOR
Instead of modifying the existing functionalities, we will
extend them.
One class takes in another class, both of which extend the
same abstract class, and adds functionality.
55. 17 . 5
17 . 6
Real example :
//First open an inputstream of it:
FileInputStream fis = new FileInputStream("/objects.gz");
//We want speed, so let's buffer it in memory:
BufferedInputStream bis = new BufferedInputStream(fis);
//The file is gzipped, so we need to ungzip it:
GzipInputStream gis = new GzipInputStream(bis);
//we can now use the read method.
56. 18 . 1
Convert the interface of a class into another interface that
client expect. The adapter pattern lets classes work together
that couldn't otherwise because of incompatible interfaces.
ADAPTER
65. 19 . 3
20
GANG OF FOUR
Behavioral patterns :
Observer, Template Method, Command, Iterator, State,
Mediator, Strategy, Chain of Responsibility, Visitor,
Interpreter, Memento.
66. 21 . 1
De ne a one-to-many dependency between objects so that
when one object changes state, all its dependents are noti ed
and updated automatically.
OBSERVER
67. 21 . 2
Example : A Graphical User Interface with 3 visualizations of
a number : binary, octal and hexadecimal. Whenever the
number changes, these visualizations must change too.
73. 22 . 1
De ne the skeleton of an algorithm in an operation, deferring
some steps to subclasses. The template method lets
subclasses rede ne certains steps of an algorithm without
changing the algorithm's structure.
TEMPLATE METHOD
74. 22 . 2
Well ... It's the purpose of an abstract class.
75. 23 . 1
Encapsulate a request as an object, thereby letting you
parameterize clients with different requests, queue or log
request, and support undoable operations.
COMMAND
81. 23 . 6
24 . 1
Provide a way to access the elements of an aggregate object
sequentially without exposing its underlying representation.
ITERATOR
82. The concerned classes (ex : People) return a subclasse
(PeopleIterator) which implements the Iterator interface.
interface Iterator<T> {
boolean hasNext();
T next();
//first();
// ...
}
83. 24 . 2
24 . 3
Remember : In Java,
List are Iterable<T>.
Set are Iterable<T>.
Arrays are not even if the foreach is well de ned.
ArrayList<String> list = new ArrayList<>();
String[] array = new String[1];
list.add("test1")
array[0] = "test1"
list.add("test2")
array[1] = "test2"
for(String s : list){
System.out.println(s);
}
for(String s : array){
System.out.println(s);
}
84. 25 . 1
Allow an object to alter its behaviour when its internal state
changes. The object will appear to change its class.
STATE
Example : Screen in a video game
87. 25 . 3
26 . 1
De ne an object that encapsulates how a set of objects
interacts. The mediator pattern promotes loose coupling by
keeping objects from referring to each other explicitly, and it
lets you vary their interaction independently.
MEDIATOR
90. 26 . 3
27 . 1
De ne a family of algorithms, encapsulate each one, and
make them interchangeable. The strategy pattern lets the
algorithm vary independently from client to client.
STRATEGY
93. 27 . 3
28 . 1
Avoid coupling the sender of a request to its receiver by
giving more than one object a chance to handle the request.
Chain the receiving objects and pass the request along the
chain until an object handles it.
CHAIN OF RESPONSIBILITY
97. 28 . 4
29 . 1
Represent an operation to be performed on the elements of
an object structure. The visitor pattern lets you de ne a new
operation without changing the classes of the elements on
which it operates.
VISITOR
101. 29 . 4
30 . 1
Given a language, de ne a representation for its grammar
along with an interpreter that uses the representation to
interpret sentences in the language.
INTERPRETER
Example : SQL parsing
105. 30 . 4
31 . 1
Without violating encapsulation, capture and externalize an
object's internal state so that the object can be restored to
this state later.
MEMENTO