Sometimes, you need to delay the instantiation of the object may be because its too costly to instantiate or it is not needed till certain point of time. You want to instantiate the object only after the basic validations. So, what you do is create an abstract layer in between the Client and object in the question. This abstract layer or the object is a proxy.
Proxy object only instantiate the object when it is required. If the object is not needed, it would not be created. Once the object is created, all the future operations would be carried over on the “Real” object instead of on the proxy object. Both – proxy and real – objects would implement the same interface. Thus both object can perform same operations seamlessly. Also the proxy object can pass all the signature to the real object.
Design Time Consideration
You should consider these step while implementing the Proxy design pattern.
- Create an interface or an Abstract class based on the requirement of the Subject.
- Implement the Interface to create a real subject
- Implement the interface to create a Proxy object. Make sure all the validation or all the operations which may not execute the real business logic. Once the object is created, simply call the methods of the real subject.