Design Pattern Space Scope Object Class Creational Structural
Design Pattern Space Scope Object Class Creational Structural Behavioural Factory Method Adaptor (Class) Interpreter Template method Abstract Factory Builder Prototype Singleton Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Adaptor (Object) Bridge Composite Decorator Flyweight Façade Proxy Design Patterns 1
The Proxy Pattern z A Structural Pattern. z Provides surrogate for another object, controls access to real target object. z Separates interface from implementation. z Real target may not always be instantiated directly due to performance, location or access restrictions. z One of the simpler patterns. Design Patterns 2
Subject {abstract} Client Real. Subject Request() Proxy Request() Design Patterns refers to ->Request() 3
Notes on Diagram z Three Classes: Subject, Real. Subject, and Proxy. z Client always operates with interface defined in {abstract} class Subject. z Interface to Subject implemented in both Real. Subject, and Proxy. z Proxy delegates any calls to Real. Subject. z Client always uses Proxy. Design Patterns 4
Example Application z. Document editor with embedded Objects, say images, expensive to create. z. Opening document should be fast, not so if all images must be loaded. z. No need to load all as not all will be viewable in Window. z. Lets create images on demand ! Design Patterns 5
Graphic Draw() Get. Extent() Store() Load() Document Editor Image image. Imp extent Draw() Get. Extent() Store() Load() image Image. Proxy file. Name extent Draw() Get. Extent() Store() Load() Design Patterns if(image= =0) image=Load. Image(file. Name); image->Draw(); if(image= =0) return(extent); else return(image->Get. Extent(); 6
Implementing in C++ class Image; extern Image* Load. An. Image. File(const char*); class Image. Ptr{ public: Image. Ptr(const char* image. File); virtual ~Image. Ptr(); virtual Image* operator->(); virtual Image& operator*(); private: Image* Load. Image(); private: Image* _image const char* _image. File }; Image. Ptr: : Image. Ptr(const char* the. Image. File){ _image. File = the. Image. File; _image = 0; } Image* Image. Ptr: : Load. Image(){ if(_image = = 0) _image = Load. An. Image. File(_image. File); return(_image); } Design Patterns 7
/*overloaded -> and * use Load. Image to return _image to callers and loading it if necessary */ Image* Image. Ptr: : operator->(){ return Load. Image(); } Image& Image. Ptr: : operator*(){ return *Load. Image(); } Design Patterns 8
/* now using this code, we can use Image operations through Image. Ptr objects without making the operations part of the Image. Ptr interface. //constructor first, set up a pointer to image, a proxy Image. Ptr image = Image. Ptr(“the. Image. File. Name”); //image is of Image class image->Draw(Point(50, 100)); //through the proxy /* here the overloaded operator -> is used to return the address of the real object and invoke the Load. Image() function which in turn calls the Load. An. Image. File() function as illustrated thus …. . */ //(image. operator->( ))->Draw(Point(50, 100)); Design Patterns 9
Notes on Implementation z Proxy acts like a pointer, yet not declared as a pointer to an image. So cannot use exactly as a pointer. z If proxy needs to know which operation is called, then overloading member access operators in this way will not work. z In this case we must manually implement each proxy operation and forward request as we did with Load. Image(). Design Patterns 10
Applicability of Pattern z Remote Proxy: provide local representative for object in different address space, may re-code request. z Virtual Proxy: creates expensive objects on demand, like in example, may also cache. z Protection Proxy: control access to original object, useful when access rights differ. Design Patterns 11
Applicability of Pattern (cont. ) z. Smart Reference: replace bare pointer, include functionality like ycounting references, deleting when no longer referenced (smart pointers). yload persistent object into memory when first referenced ycontrol locking of object before access. Design Patterns 12
Patterns in UML z Collaboration Diagrams (interaction) document Design Patterns in UML. z Context (Object Diagram) and interaction are described. a. Client: Client a. Proxy: Proxy Object Diagram describes context a. Client: Client 1: Request() a. Proxy: Proxy Collaboration Diagram describes interaction Design Patterns a. Real. Subject: Subject 1. 1: Request() a. Real. Subject: Subject {new} 13
a. Client: Client a. Proxy: Proxy a. Real. Subject: Subject Sequence Diagram also describes interaction Design Patterns 14
Design Patterns: UML Parameterised Collaborations Sales Interface Sales proxy subject Proxy Sales Statistics realsubject Design Patterns The Classes are the participants in the Parameterised Collaboration. 15
Roles in the Collaboration z In the Proxy pattern y. Sales Interface plays the role of subject. y. Sales Statistics plays the role of realsubject. y. Sales plays the role of proxy. z Dependency annotated by dashed arrows. Design Patterns 16
Sales Interface {abstract} Client Sales Statistics Sales refers. To Sum() Design Patterns refers. To->Sum() 17
C++ Node Interface #ifndef _NODE_H //this is “Node. h” #define _NODE_H class Tree; //forward class Node{ //describe vertices friend class Tree; protected: Node(void): use_(1){} virtual void print(ostream&)const=0; //pure virtual ~Node(void); //important to make virtual private: int use_; //reference counter }; #endif Design Patterns 18
- Slides: 18