Proxy Pattern

2:32 AM Shashank Tiwary 0 Comments

Intent:
                    The pattern Provide a surrogate or placeholder for another object to control access to it. The proxy object have the code for access control that will be executed before calling the real function of its base class.(Not understand read after finished the article).

Motivating Example:
                    The pattern specially need while you need a placeholder for an actual object that is expensive to create. For example a document containing lots of images or bulky objects which are expensive in use instead of that you can replace these bulky objects with proxy objects which will create the real objects while needed. This example is very much similar with flyweight pattern. In flyweight pattern you considered with intrinsic and extrinsic element of the object and the object does not need another substitution.

                    One more example would be when you need to interact with a remote service over the network, but want to keep your code decoupled from networking detail. (It could be demonstrated by WCF service).

Applicability:
                    The Proxy pattern is applicable in several common scenario:
  • A remote proxy acts as local representative of remote object, and abstracts away the details of communicating with remote object.
  • A virtual proxy creates expensive objects on demand. A common example is an Image Proxy that show a placeholder while the Image is being loaded.
  • A Protection Proxy can be used to control access to an object, based on authorization rules.
Structure:
How It Gets Used:

  • Clients work with Proxy as if it were the actual object. Usually the proxy object have common interface with real object.
  • This pattern also use to control the access to the actual object and delegated the calls to it as needed. This also mean that Proxy pattern support the Open Close system i.e., the object is open for extension but close for modification.
  • This pattern is use to- Improve performance and load time of an application, Simplify interactions with with remote objects, defer expensive calls until needed- implement lazy loading of objects from persistence.   
Implementation Example: 

Consequences:
                      The significance of Proxy Pattern are :
  • Client code does not need to be changed to work with a proxy object because proxy will give the same interface or structure like real object when replacing the real object. However, proxy object must be synchronized with the real object. The proxy object adsorb the change in real object and continue to support the same interface expose to the client. 
  • Proxy may be used to optimize performance in existing system without touching existing class behavior this means that proxy pattern support Open/Close Principle i.e., extension can be possible but modification can't.
  • Client code may make incorrect assumption about behavior of real object as proxy object masked the real object behavior. The interface use by the proxy may describe the interface methods in such a way that it may use many calls to real object method rather than few, large call. This gives rise born of the two different interface which may be implemented by the proxy pattern one is chatty another is chunky. While use Chatty type interface proxy do many small calling to real object method and while use Chunky type interface proxy do less number of calling to real object method.
  • A disadvantage of using chatty interface is that it may required more database calls than necessary.
I hope that this article benefit you Thanks for reading.
Please feel free to contact me lxbalder@gmail.com       

0 comments: