Design patterns - Proxy & Composite

67
Design Paerns Proxy & Composite @sarat, architect Experion Technologies

description

Explains foundations of proxy and composite patterns

Transcript of Design patterns - Proxy & Composite

Page 1: Design patterns - Proxy & Composite

Design Patterns Proxy & Composite

@sarat, architect Experion Technologies

Page 2: Design patterns - Proxy & Composite

composite pattern

Page 3: Design patterns - Proxy & Composite

is a structural pattern

Page 4: Design patterns - Proxy & Composite

Structural design patterns ease the design by identifying a simple way

to realize relationships between entities

Page 5: Design patterns - Proxy & Composite

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

Page 6: Design patterns - Proxy & Composite

Leaf (primitive element)

• represents leaf objects in the composition.

• A leaf has no children.

• defines behavior for primitive objects in the composition

Page 7: Design patterns - Proxy & Composite

Composite

• defines behavior for components having children.

• stores child components.

• implements child-related operations in the Component interface.

Page 8: Design patterns - Proxy & Composite

Client

!

• manipulates objects in the composition through the Component interface.

Page 9: Design patterns - Proxy & Composite
Page 10: Design patterns - Proxy & Composite

/** "Component" */interface Graphic { //Prints the graphic. public void print();}

Page 11: Design patterns - Proxy & Composite

class CompositeGraphic implements Graphic { //Collection of child graphics. private List<Graphic> childGraphics = new ArrayList<Graphic>();

Page 12: Design patterns - Proxy & Composite

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(); } }

Page 13: Design patterns - Proxy & Composite

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); }}

Page 14: Design patterns - Proxy & Composite

/** "Leaf" */class Ellipse implements Graphic { //Prints the graphic. public void print() { System.out.println("Ellipse"); }}

Page 15: Design patterns - Proxy & Composite

/** 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();

Page 16: Design patterns - Proxy & Composite

/** 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();

Page 17: Design patterns - Proxy & Composite

/** 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);

Page 18: Design patterns - Proxy & Composite

/** 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);

Page 19: Design patterns - Proxy & Composite

/** 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();}

Page 20: Design patterns - Proxy & Composite

Questions?

Page 21: Design patterns - Proxy & Composite

What’s proxy pattern?

Page 22: Design patterns - Proxy & Composite

it makes the clients of a component communicate with a representative rather than to the component Itself.

Page 23: Design patterns - Proxy & Composite

enables enhanced efficiency, easier access and protection from unauthorized access.

Page 24: Design patterns - Proxy & Composite

examples?

Page 25: Design patterns - Proxy & Composite

a network proxy

Page 26: Design patterns - Proxy & Composite

large object in memory

Page 27: Design patterns - Proxy & Composite

reference counting pointer objects

Page 28: Design patterns - Proxy & Composite

Context

• A client needs access to the services of another component

• Direct access is technically possible, but may not be the best approach

Page 29: Design patterns - Proxy & Composite

Problem

• Often it’s inappropriate to access the component directly

• Direct and unrestricted access can be insecure and inefficient

Page 30: Design patterns - Proxy & Composite

Key considerations

Page 31: Design patterns - Proxy & Composite

run-time efficient

Page 32: Design patterns - Proxy & Composite

cost effective

Page 33: Design patterns - Proxy & Composite

safe for both client and component

Page 34: Design patterns - Proxy & Composite

transparent interfaces

Page 35: Design patterns - Proxy & Composite

should be similar to component’s interface

Page 36: Design patterns - Proxy & Composite

aware of performance penalties

Page 37: Design patterns - Proxy & Composite

solution

Page 38: Design patterns - Proxy & Composite

communicate to representative — proxy

Page 39: Design patterns - Proxy & Composite

proxy performs pre & post processing (e.g

access control)

Page 40: Design patterns - Proxy & Composite

structure

Page 41: Design patterns - Proxy & Composite

uses proxy to fulfill it’s task

Client

Page 42: Design patterns - Proxy & Composite

a common interface for proxy and original

AbstractOriginal

Page 43: Design patterns - Proxy & Composite

provides interface of the original to clients

proxy

Page 44: Design patterns - Proxy & Composite

ensures safe, efficient and correct access to the

original

proxy

Page 45: Design patterns - Proxy & Composite

collaborates with original

proxy

Page 46: Design patterns - Proxy & Composite

implements a particular service

Original

Page 47: Design patterns - Proxy & Composite
Page 48: Design patterns - Proxy & Composite

dynamics

Page 49: Design patterns - Proxy & Composite
Page 50: Design patterns - Proxy & Composite

implementation considerations

Page 51: Design patterns - Proxy & Composite

migrate all client responsibilities to proxy

Page 52: Design patterns - Proxy & Composite

remove all direct relationship with client

and original

Page 53: Design patterns - Proxy & Composite

variants

Page 54: Design patterns - Proxy & Composite

To shield network addresses and inter-process communication protocols from clients

Remote Proxy

Page 55: Design patterns - Proxy & Composite

Access authorization e.g network authorization to use Internet

Protection Proxy

Page 56: Design patterns - Proxy & Composite

Multiple local components can share results from a remote proxye.g. Cache server

Cache Proxy

Page 57: Design patterns - Proxy & Composite

Provide synchronized access to services in a multi-access or multi-threaded environment

Synchronization Proxy

Page 58: Design patterns - Proxy & Composite

Prevents accidental deletion of components or collects usage statistics

e.g. reference counting objects

Counting Proxy

Page 59: Design patterns - Proxy & Composite

Protects local clients from outside world

Firewall proxy

Page 60: Design patterns - Proxy & Composite

benefits

Page 61: Design patterns - Proxy & Composite

enhanced efficiency and lower cost

e.g. cache proxy

Page 62: Design patterns - Proxy & Composite

decouple clients from real objects

Page 63: Design patterns - Proxy & Composite

separate housekeeping from functionality

Page 64: Design patterns - Proxy & Composite

liabilities

Page 65: Design patterns - Proxy & Composite

less efficiency due to indirection

Page 66: Design patterns - Proxy & Composite

overkill using sophisticated strategies !

e.g. caching server in a dynamic environment

Page 67: Design patterns - Proxy & Composite

Questions?