KEMBAR78
Design patterns - Proxy & Composite | PDF
Design Patterns
Proxy & Composite
@sarat, architect
Experion Technologies
composite pattern
is a structural pattern
Structural design patterns ease the
design by identifying a simple way
to realize relationships between
entities
Compose objects into tree structures to represent
part-whole hierarchies.
!
describes that a group of objects are to be treated
in the same way as a single instance of an object
Leaf (primitive element)
• represents leaf objects in the composition.
• A leaf has no children.
• defines behavior for primitive objects in the
composition
Composite
• defines behavior for components having children.
• stores child components.
• implements child-related operations in the
Component interface.
Client
!
• manipulates objects in the composition through the
Component interface.
/** "Component" */	
interface Graphic {	
	
//Prints the graphic.	
public void print();	
}
class CompositeGraphic implements Graphic {	
	
//Collection of child graphics.	
private List<Graphic> childGraphics = new ArrayList<Graphic>();
class CompositeGraphic implements Graphic {	
	
//Collection of child graphics.	
private List<Graphic> childGraphics = new ArrayList<Graphic>();	
!
//Prints the graphic.	
public void print() {	
for (Graphic graphic : childGraphics) {	
graphic.print();	
}	
}
class CompositeGraphic implements Graphic {	
	
//Collection of child graphics.	
private List<Graphic> childGraphics = new ArrayList<Graphic>();	
!
//Prints the graphic.	
public void print() {	
for (Graphic graphic : childGraphics) {	
graphic.print();	
}	
} 	
!
//Adds the graphic to the composition.	
public void add(Graphic graphic) {	
childGraphics.add(graphic);	
}	
	
//Removes the graphic from the composition.	
public void remove(Graphic graphic) {	
childGraphics.remove(graphic);	
}	
}
/** "Leaf" */	
class Ellipse implements Graphic {	
	
//Prints the graphic.	
public void print() {	
System.out.println("Ellipse");	
}	
}
/** Client */	
public class Program {	
	
public static void main(String[] args) {	
//Initialize four ellipses	
Ellipse ellipse1 = new Ellipse();	
Ellipse ellipse2 = new Ellipse();	
Ellipse ellipse3 = new Ellipse();	
Ellipse ellipse4 = new Ellipse();
/** Client */	
public class Program {	
	
public static void main(String[] args) {	
//Initialize four ellipses	
Ellipse ellipse1 = new Ellipse();	
Ellipse ellipse2 = new Ellipse();	
Ellipse ellipse3 = new Ellipse();	
Ellipse ellipse4 = new Ellipse();	
!
//Initialize three composite graphics	
CompositeGraphic graphic = new CompositeGraphic();	
CompositeGraphic graphic1 = new CompositeGraphic();	
CompositeGraphic graphic2 = new CompositeGraphic();
/** Client */	
public class Program {	
	
public static void main(String[] args) {	
//Initialize four ellipses	
Ellipse ellipse1 = new Ellipse();	
Ellipse ellipse2 = new Ellipse();	
Ellipse ellipse3 = new Ellipse();	
Ellipse ellipse4 = new Ellipse();	
!
//Initialize three composite graphics	
CompositeGraphic graphic = new CompositeGraphic();	
CompositeGraphic graphic1 = new CompositeGraphic();	
CompositeGraphic graphic2 = new CompositeGraphic();	
!
//Composes the graphics	
graphic1.add(ellipse1);	
graphic1.add(ellipse2);	
graphic1.add(ellipse3);	
	
graphic2.add(ellipse4);
/** Client */	
public class Program {	
	
public static void main(String[] args) {	
//Initialize four ellipses	
	 	 	 …	
!
//Initialize three composite graphics	
	 	 	 …	
!
//Composes the graphics	
graphic1.add(ellipse1);	
graphic1.add(ellipse2);	
graphic1.add(ellipse3);	
	
graphic2.add(ellipse4);	
graphic.add(graphic1);	
graphic.add(graphic2);	
!
graphic.add(graphic1);	
graphic.add(graphic2);
/** Client */	
public class Program {	
	
public static void main(String[] args) {	
//Initialize four ellipses	
	 	 	 …	
!
//Initialize three composite graphics	
	 	 	 …	
!
//Composes the graphics	
	 	 	 …	
	
	 	 	 …	
!
	 	 	 …	
!
//Prints the complete graphic (4 times "Ellipse").	
graphic.print();	
}
Questions?
What’s proxy pattern?
it makes the clients of a component
communicate with a representative
rather than to the component Itself.
enables enhanced efficiency,
easier access and protection
from unauthorized access.
examples?
a network proxy
large object in memory
reference counting
pointer objects
Context
• A client needs access to the services of
another component
• Direct access is technically possible, but
may not be the best approach
Problem
• Often it’s inappropriate to access the
component directly
• Direct and unrestricted access can be
insecure and inefficient
Key considerations
run-time efficient
cost effective
safe for both client and
component
transparent interfaces
should be similar to
component’s interface
aware of performance
penalties
solution
communicate to
representative — proxy
proxy performs
pre & post processing (e.g
access control)
structure
uses proxy to fulfill it’s
task
Client
a common interface for
proxy and original
AbstractOriginal
provides interface of the
original to clients
proxy
ensures safe, efficient and
correct access to the
original
proxy
collaborates with
original
proxy
implements a particular
service
Original
dynamics
implementation
considerations
migrate all client
responsibilities to proxy
remove all direct
relationship with client
and original
variants
To shield network addresses and inter-process communication
protocols from clients
Remote Proxy
Access authorization 

e.g network authorization to use Internet
Protection Proxy
Multiple local components can share results from a remote proxy

e.g. Cache server
Cache Proxy
Provide synchronized access to services in a multi-access or multi-
threaded environment
Synchronization Proxy
Prevents accidental deletion of components or collects usage
statistics

e.g. reference counting objects
Counting Proxy
Protects local clients from outside world
Firewall proxy
benefits
enhanced efficiency and
lower cost
e.g. cache proxy
decouple clients from
real objects
separate housekeeping
from functionality
liabilities
less efficiency due to
indirection
overkill using sophisticated strategies
!
e.g. caching server in a dynamic
environment
Questions?

Design patterns - Proxy & Composite