1 Advanced OOSE Proxy Pattern Dr. Abeer Hamdy
2 Proxy Pattern Intent : Provide a surrogate or placeholder for another object to control access to it. Motivation: Some reasons for a proxy are: There are situations in which a client does not or can not reference an object directly, but wants to still interact with the object. A proxy object can act as the intermediary between the client and the target object. to defer the full cost of creation and initialization a memory intensive object until it is actually needed. To control access to an object.
3 Proxy Pattern possible object diagram of a proxy structure at run-time : Proxy structure:
4 Types of Proxies Remote Proxy - Provides a reference to an object located in a different address space on the same or different machine Virtual Proxy - Allows the creation of a memory intensive object on demand. The object will not be created until it is really needed. Protection (Access) Proxy - Provides different clients with different levels of access to a target object
5 Remote Proxy Scenario: A machine has several services running on well-known ports. We want to be able to access these services from various client machines as if they were local objects. How can this be accomplished? Solution: Use a Remote Proxy Remote proxy is the essence of distributed object technology such as Java RMI, Jini, CORBA,etc.
6 Virtual Proxy Pattern Scenario: Consider a document editor that can embed graphical objects in a document. Some graphical objects, like large raster images, can be expensive to create. But opening a document should be fast, so we should avoid creating all the expensive objects at once when the document is opened. As not all of these objects will be visible in the document at the same time. I.e. creating each expensive object on demand, which in this case occurs when an image becomes visible. But what do we put in the document in place of the image? And how can we hide the fact that the image is created on demand so that we don't complicate the editor's implementation?
7 Proxy Pattern Scenario ( cont. ): The solution is to use another object, an “image proxy”, that acts as a stand-in for the real image. The proxy acts just like the image and takes care of instantiating it when it's required. The image proxy creates the real image only when the document editor asks it to display itself by invoking its Draw operation. The proxy forwards subsequent requests directly to the image. It must therefore keep a reference to the image after creating it.
8 Virtual Proxy Pattern Scenario ( cont. ): Class diagram of the example
9 References Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson and Vlissides, addison-Wesley, 1995