260 likes | 716 Views
Proxy Design Pattern. By:Diksha Agarwal. Definition. Provide a surrogate or placeholder for another object to control access to it. Intent.
E N D
Proxy Design Pattern By:Diksha Agarwal
Definition • Provide a surrogate or placeholder for another object to control access to it.
Intent • Proxy pattern describe how to provide a level of indirection to an object, and the implementations of the proxy object keep a reference to another object to which they forwards requests. • Proxy pattern composes an object and provides an identical interface to clients. • Its intent is to provide a stand-in for a subject when it's inconvenient or undesirable to access the subject directly because, for example, it lives on a remote machine or has restricted access.
Motivation • One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it.
Applicability • A proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. A remote proxy provides a local representation for an object in a different address space. It provides an interface identical to the subjects, controls access to the real subject and also may be responsible for creating and deleting it. A remote proxy is responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
Participation • Proxy - Maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same. - Provides an interface identical to Subject's so that a proxy can by substituted for real subject. - Controls access to the real subject and may be responsible for creating and deleting it. other responsibilities depend on the kind of proxy. • Subject - Defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected. • RealSubject - Defines the real object that the proxy represents.
Example- Payment By Cheque • The Problem: A person has cash in his bank account but does not carry cash with him. • The Solution: The proxy Design Pattern.
Example Contd.. • The cheque acts as a proxy for the funds in the account. • The funds are the real subjects. • The client asks for payment through the Cheque. • The proxy allows the cheque to act as if it were actual funds.
Related Patterns: • Adapter provides a different interface to the object it adapts, while proxy provides the same interface as the subject. • Decorators can have similar implementations as proxies but they have a different purpose; A decorator adds one or more responsibilities to an object, whereas proxy controls access to an object.
Costs Incurred… • There are no real costs associated with the proxy pattern. • Proxy allows for the saving of run time costs by deferring processing of objects until it is actually required.
Conclusion: • Proxy creates a level of indirection that can have multiple purposes. • The major uses of this design pattern are: • The remote proxy hides the fact that the object is in different object space. • A virtual proxy can create optimizations such as creating an object on demand. • An access proxy allows additional housekeeping tasks when an object is accessed.
References: • http://www.inf.bme.hu/ooret/1999osz/DesignPatterns/Proxy4/ • http://web.media.mit.edu/~tpminka/patterns/Proxy.html • http://selab.korea.ac.kr/selab/courses/GoF-patterns/proxy.htm#Structure • http://pages.cpsc.ucalgary.ca/~heinrich/443/TheFlyweightandProxyDesignPatterns.ppt