Proxy Design Pattern

It enables us to avoid the creation of resource consuming object until its actual need in the system. A complex and heavy object can be represented by a simple proxy object which creates the illusion of actual object till the actual requirement of that object. The proxy object initializes the actual object whenever it gets the first request from client. So it delays the initialization of heavy states until these are actually required.

Example

  • In ORM tools, collections having a large number of items used to be initialized lazily using proxy pattern. Before actual initialization, only a proxy object creates the illusion of actual object.

Usage

  • Helps to avoid heavy object in memory before their actual requirement.
  • Helps to avoid heavy resource consuming calls like database access for large data until unless it is not required.

When to Use

  • When states of a heavy object can be initialized lazily, and are not bound with the instantiation of container object.
  • When the contained data needs a lot of memory and may also consume a lot  resources for its initialization
  • When a resource consuming states needs to be initialized by some other system like database system or file system and are not saved as integral part of container object.
  • If the object is on remote machine and loading it takes a lot of time

Drawbacks


  • Special care need to be given if the container object is used as data transfer object across the tiers. In this scenario, the states of the proxy object must be initialized before detaching it from originating tier.

People who read this post also read :



0 comments:

Post a Comment