design patterns - proxy & composite
DESCRIPTION
Explains foundations of proxy and composite patternsTRANSCRIPT
![Page 1: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/1.jpg)
Design Patterns Proxy & Composite
@sarat, architect Experion Technologies
![Page 2: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/2.jpg)
composite pattern
![Page 3: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/3.jpg)
is a structural pattern
![Page 4: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/4.jpg)
Structural design patterns ease the design by identifying a simple way
to realize relationships between entities
![Page 5: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/7.jpg)
Composite
• defines behavior for components having children.
• stores child components.
• implements child-related operations in the Component interface.
![Page 8: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/8.jpg)
Client
!
• manipulates objects in the composition through the Component interface.
![Page 9: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/9.jpg)
![Page 10: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/10.jpg)
/** "Component" */interface Graphic { //Prints the graphic. public void print();}
![Page 11: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/11.jpg)
class CompositeGraphic implements Graphic { //Collection of child graphics. private List<Graphic> childGraphics = new ArrayList<Graphic>();
![Page 12: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/12.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/14.jpg)
/** "Leaf" */class Ellipse implements Graphic { //Prints the graphic. public void print() { System.out.println("Ellipse"); }}
![Page 15: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/15.jpg)
/** 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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/16.jpg)
/** 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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/17.jpg)
/** 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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/18.jpg)
/** 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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/19.jpg)
/** 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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/20.jpg)
Questions?
![Page 21: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/21.jpg)
What’s proxy pattern?
![Page 22: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/22.jpg)
it makes the clients of a component communicate with a representative rather than to the component Itself.
![Page 23: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/23.jpg)
enables enhanced efficiency, easier access and protection from unauthorized access.
![Page 24: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/24.jpg)
examples?
![Page 25: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/25.jpg)
a network proxy
![Page 26: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/26.jpg)
large object in memory
![Page 27: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/27.jpg)
reference counting pointer objects
![Page 28: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/28.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/29.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/30.jpg)
Key considerations
![Page 31: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/31.jpg)
run-time efficient
![Page 32: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/32.jpg)
cost effective
![Page 33: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/33.jpg)
safe for both client and component
![Page 34: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/34.jpg)
transparent interfaces
![Page 35: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/35.jpg)
should be similar to component’s interface
![Page 36: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/36.jpg)
aware of performance penalties
![Page 37: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/37.jpg)
solution
![Page 38: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/38.jpg)
communicate to representative — proxy
![Page 39: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/39.jpg)
proxy performs pre & post processing (e.g
access control)
![Page 40: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/40.jpg)
structure
![Page 41: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/41.jpg)
uses proxy to fulfill it’s task
Client
![Page 42: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/42.jpg)
a common interface for proxy and original
AbstractOriginal
![Page 43: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/43.jpg)
provides interface of the original to clients
proxy
![Page 44: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/44.jpg)
ensures safe, efficient and correct access to the
original
proxy
![Page 45: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/45.jpg)
collaborates with original
proxy
![Page 46: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/46.jpg)
implements a particular service
Original
![Page 47: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/47.jpg)
![Page 48: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/48.jpg)
dynamics
![Page 49: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/49.jpg)
![Page 50: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/50.jpg)
implementation considerations
![Page 51: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/51.jpg)
migrate all client responsibilities to proxy
![Page 52: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/52.jpg)
remove all direct relationship with client
and original
![Page 53: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/53.jpg)
variants
![Page 54: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/54.jpg)
To shield network addresses and inter-process communication protocols from clients
Remote Proxy
![Page 55: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/55.jpg)
Access authorization e.g network authorization to use Internet
Protection Proxy
![Page 56: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/56.jpg)
Multiple local components can share results from a remote proxye.g. Cache server
Cache Proxy
![Page 57: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/57.jpg)
Provide synchronized access to services in a multi-access or multi-threaded environment
Synchronization Proxy
![Page 58: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/58.jpg)
Prevents accidental deletion of components or collects usage statistics
e.g. reference counting objects
Counting Proxy
![Page 59: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/59.jpg)
Protects local clients from outside world
Firewall proxy
![Page 60: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/60.jpg)
benefits
![Page 61: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/61.jpg)
enhanced efficiency and lower cost
e.g. cache proxy
![Page 62: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/62.jpg)
decouple clients from real objects
![Page 63: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/63.jpg)
separate housekeeping from functionality
![Page 64: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/64.jpg)
liabilities
![Page 65: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/65.jpg)
less efficiency due to indirection
![Page 66: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/66.jpg)
overkill using sophisticated strategies !
e.g. caching server in a dynamic environment
![Page 67: Design patterns - Proxy & Composite](https://reader033.vdocument.in/reader033/viewer/2022051412/54c74ecf4a795959108b4588/html5/thumbnails/67.jpg)
Questions?