Ip07 PDF
Ip07 PDF
1
Din Cursurile trecute…
Design Patterns
◦ Elements
◦ GOF
◦ Creational Patterns
Creational Patterns (not in GOF)
◦ Lazy Initialization
◦ Object Pool
Structural Patterns
◦ Adapter
◦ Bridge
◦ Composite
◦ Decorator
◦ Façade
◦ Flyweight
◦ Proxy
2
Design Patterns: Definitions, Elements
Creational Patterns
◦ Abstract Factory
◦ Builder
◦ Factory Method
◦ Prototype
◦ Singleton
3
Abstract Factory – maze, computer components
4
Factory Method – Open/New Project, Hello Mr/Ms
5
Builder – RTF Reader, Happy Meal
6
Prototype – Music editor, Clonable
7
Singleton - Logger
8
Lazy initialization is the tactic of delaying the
creation of an object, the calculation of a
value, or some other expensive process until
the first time it is needed
9
public class Fruit {
private static final Map<String,Fruit> types = new
HashMap<String, Fruit>();
private final String type;
public Policy() {
this.salesPerson = new SalesPerson();
} 11
A2: You could use lazy initialization to delay the
creation until the SalesPerson object is actually
needed
public Policy() {
}
public SalesPerson getSalesPerson() {
if ( this.salesPerson == null ) {
this.salesPerson = new SalesPerson();
}
return this.salesPerson;
} 12
Intent: reuse and share objects that are
expensive to create.
14
15
Lets’ take the example of the database
connections. It's obviously that opening too
many connections might affect the performance:
◦ Creating a connection is an expensive operation
◦ When there are too many connections opened it
takes longer to create a new one and the
database server will become overloaded
17
Rather than composing interfaces or
implementations, structural object
patterns describe ways to compose
objects to realize new functionality
The added flexibility of object
composition comes from the ability to
change the composition at run-time
18
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
19
Intent: Convert the interface of a class into another
interface clients expect. Adapter lets classes work
together that couldn't otherwise because of
incompatible interfaces.
Also Known As: Wrapper
Motivation: Consider for example a drawing editor
that lets users draw and arrange graphical elements
(lines, polygons, text, etc.) into pictures and
diagrams. The interface for graphical objects is
defined by an abstract class called Shape. The editor
defines a subclass of Shape for each kind of
graphical object: a LineShape class for lines, a
PolygonShape class for polygons, and so forth.
20
A TextShape subclass that can display and edit text is
considerably more difficult to implement
We suppose the existence of a TextView class for
displaying and editing text. So we can consider TextShape
derived from these classes
21
Applicability - Use the Adapter pattern when
◦ you want to use an existing class, and its interface
does not match the one you need
22
Structure - The Adapter pattern is used so that two
unrelated interfaces can work together. This is
something like we convert interface of one class into
interface expected by the client.
23
We all have electric sockets in our houses of
different sizes and shapes. I will take an
example of a socket of 15 Ampere. This is a
bigger socket and the other one which is smaller
is of 5 Ampere. A 15 Amp plug cannot fit into a
5 Amp socket => we will use an Adapter
26
Intent - Decouple an abstraction from its
implementation so that the two can vary
independently
Also Known As - Handle/Body
Motivation - Consider the abstraction of shapes,
each with its own properties. One thing all
shapes can do is draw themselves. Drawing
graphics to a screen can be dependent on
different graphics implementations or operating
systems. Shapes have to be able to be drawn on
many types of operating systems
27
The bridge helps by allowing the creation of new implementation
classes that provide the drawing implementation. Shape class
provides methods for getting the size or properties of a shape.
Drawing class provides an interface for drawing graphics
28
interface DrawingAPI {/** "Implementor" */
public void drawCircle(double x, double y, double
radius);
}
/** "ConcreteImplementor" 1,2 */
class DrawingAPI1 implements DrawingAPI {
public void drawCircle(double x, double y, double
radius) { System.out.printf("API1” + x + y + radius);}
}
class DrawingAPI2 implements DrawingAPI {
public void drawCircle(double x, double y, double
radius) { System.out.printf("API2“ + x + y + radius);}
} 29
interface Shape {/** "Abstraction" */
public void draw();
public void resizeByPercentage(double pct); }
/** "Refined Abstraction" */
class CircleShape implements Shape {
private double x, y, radius;
private DrawingAPI drawingAPI;
public CircleShape(double x, double y, double radius,
DrawingAPI drawingAPI) {
this.x = x; this.y = y; this.radius = radius;
this.drawingAPI = drawingAPI;
}
public void draw() {drawingAPI.drawCircle(x, y, radius); }
public void resizeByPercentage(double pct) { radius *= pct; }
}
30
/** "Client" */
class BridgePattern {
public static void main(String[] args) {
Shape[] shapes = new Shape[2];
shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1());
shapes[1] = new CircleShape(5, 7, 8, new DrawingAPI2());
for (Shape shape : shapes) {
shape.resizeByPercentage(2.5);
shape.draw();
}
}
}
31
Intent - Compose objects into tree structures to
represent part-whole hierarchies. Composite
lets clients treat individual objects and
compositions of objects uniformly
32
Graphics applications like drawing editors and schematic
capture systems let users build complex diagrams out of
simple components
33
Applicability - Use this pattern when
◦ you want to represent part-whole hierarchies of
objects
34
Structure
35
The most common example in this pattern is of
a company’s employee hierarchy
36
public class Employee {
private String name; private double salary;
private Vector subordinates;
CFO.add(headFinance1); CFO.add(headFinance2);
headFinance1.add(accountant1);headFinance1.add(accountant4);
headFinance2.add(accountant2);headFinance2.add(accountant3);
}
39
Intent - Attach additional responsibilities to an
object dynamically. Decorators provide a flexible
alternative to subclassing for extending
functionality
Also Known As – Wrapper (similar Adapter)
Motivation - Sometimes we want to add
responsibilities to individual objects, not to an
entire class. A graphical user interface toolkit,
for example, should let you add properties like
borders or behaviors like scrolling to any user
interface component
40
A more flexible approach is to enclose the component
in another object that adds the border. The enclosing
object is called a decorator
41
Structure
42
Applicability - Use Decorator
◦ to add responsibilities to individual objects
43
Suppose we have some 6 objects and 2 of them
need a special behavior, we can do this with the
help of a decorator
44
public abstract class Decorator {
/** The method places each decorative item on
the tree. */
public abstract void place(Branch branch);
}
public class ChristmasTree {
private Branch branch;
public Branch getBranch() {
return branch;
}
}
45
public class BallDecorator extends Decorator {
public BallDecorator(ChristmasTree tree) {
Branch branch = tree.getBranch();
place(branch);
}
46
Similarly, we can make StarDecorator and
RufflesDecorator
47
Intent - Provide a unified interface to a set of
interfaces in a subsystem
Motivation - Consider for example a
programming environment that gives
applications access to its compiler subsystem.
This subsystem contains classes such as
Scanner, Parser, ProgramNode, BytecodeStream,
and ProgramNodeBuilder that implement the
compiler. Some specialized applications might
need to access these classes directly. But most
clients of a compiler want to compile some code
48
A common design goal is to minimize the communication and
dependencies between subsystems
49
The compiler facade makes life easier for most programmers without
hiding the lower-level functionality from the few that need it
50
Applicability - Use the Facade pattern when
◦ you want to provide a simple interface to a complex
subsystem
◦ there are many dependencies between clients and the
implementation classes of an abstraction
◦ you want to layer your subsystems
51
Facade as the name suggests means the face of
the building. The people walking past the road
can only see this glass face of the building. The
face hides all the complexities of the building
and displays a friendly face.
Facade hides the complexities of the system and
provides an interface to the client from where
the client can access the system. In Java, the
interface JDBC can be called a façade
Other examples?
52
Let’s consider a store. This store has a store
keeper. In the storage, there are a lot of
things stored e.g. packing material, raw
material and finished goods.
You, as client want access to different goods.
You do not know where the different
materials are stored. You just have access to
store keeper who knows his store well. Here,
the store keeper acts as the facade, as he
hides the complexities of the system Store.
53
public interface Store {
public Goods getGoods();
}
55
public class Client {
public static void main(String[] args) {
StoreKeeper keeper = new StoreKeeper();
RawMaterialGoods rawMaterialGoods =
keeper.getRawMaterialGoods();
}
}
56
Intent - Use sharing to support large numbers of
fine-grained objects efficiently
57
The following diagram shows how a document editor
can use objects to represent characters
58
59
A Flyweight is an object that minimizes memory
use by sharing as much data as possible with other
similar objects
64
The following class diagram illustrates this example in
more detail
65
Let’ say we need to withdraw money to make some
purchase. The way we will do it is, go to an ATM
and get the money, or purchase straight with a
cheque.
In old days when ATMs and cheques were not
available, what used to be the way??? Well, get your
passbook, go to bank, get withdrawal form there,
stand in a queue and withdraw money. Then go to
the shop where you want to make the purchase.
In this way, we can say that ATM or cheque in
modern times act as proxies to the Bank.
66
public class Bank {
private int numberInQueue;
if(isBalanceAvailable)
return amountNeeded;
else
return 0;
} 68
Erich Gamma, Richard Helm, Ralph Johnson,
and John Vlissides: Design Patterns: Elements
of Reusable Object-Oriented Software
(GangOfFour)
69
Design Patterns: http://www.oodesign.com/
Gang-Of-Four: http://c2.com/cgi/wiki?GangOfFour,
http://www.uml.org.cn/c%2B%2B/pdf/DesignPatterns.pdf
Design Patterns Book: http://c2.com/cgi/wiki?DesignPatternsBook
About Design Patterns: http://www.javacamp.org/designPattern/
Design Patterns – Java companion:
http://www.patterndepot.com/put/8/JavaPatterns.htm
Java Design patterns:
http://www.allapplabs.com/java_design_patterns/java_design_patter
ns.htm
Overview of Design Patterns:
http://www.mindspring.com/~mgrand/pattern_synopses.htm
Lazy initialization: http://en.wikipedia.org/wiki/Lazy_initialization
Use Lazy Initialization to Conserve Resources:
http://www.devx.com/tips/Tip/18007
70