Proxy Design Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Continuing our series of design pattern, we will learn yet another i.e. proxy design pattern implementation in this article. It is a structural design pattern which acts as a placeholder for other objects.

 

Proxy Design Pattern Implementation:

Gang of Four defines it as

Proxy design pattern provides a placeholder for an object to control references to it.

  • Sometimes we want to control access to a specific object.
  • Some objects may have heavy work going in them and we can call only those methods which are required at that time. This can improve performance.
  • Proxy can act as a wrapper to another object.

Generic Class Diagram:

Proxy Design Pattern

Proxy Design Pattern

Components involved are

  • Subject: This is an interface which needs to be implemented by real object as well as proxy object. Both represent services provided by Subject.
  • ProxySubject: Maintains reference to RealObject, can perform other operations before or after calling RealObject, operations and can act as a wrapper to RealObject.
  • RealObject: Actual object of which proxy we are going to created. Proxies can be needed because RealObject may perform some heavy operations which may not be needed all the time.

Proxy Design Pattern Implementation Example:

  • We have a BikeMechanic interface which has service() method.
  • RealBikeMechanic implements BikeMechanic and implements service() method. It also has dismantleBike() which is called from constructor directly (Our mechanic is overly enthusiastic and dismantles everything as soon as he gets it)
  • Now this task of dismantling takes lot of time and it is not needed everytime for servicing. What do we do now?
  • Our solution can be to create a proxy for RealBikeMechanic which will be able to call service() method without having to go through heavy task of dismantling the bike (which happens as soon as RealBikeMechanic constructor is invoked)
  • This way, we can invoke service() method without even instantiating RealBikeMechanic directly.
  • We will instantiate BikeMechanicProxy and perform operations on it.

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout proxy

If you don’t have git installed, you can either directly download zip or copy paste from below.

Bike.java

 

BikeMechanic.java

RealBikeMechanic.java

Notice that dismantle() method is being called from constructor which doesn’t have any connection with service(). It consumes a lot of time. So every time object is created, it is going to consume lot of time.

BikeMechanicProxy.java

Notice here that task of dismantling bike is not needed. Each instance creation does not consume much time. service() method is implemented such that heavy operations are going to get executed only when bike servicing is to be done. Creation of proxy objects does not consume time as compared to real objects. We can test our proxy design pattern implementation with below code.

Output:

Proxy Design Pattern Implementation Diagram:

Proxy Design Pattern Example

Proxy Design Pattern Example

I hope this article helped understand proxy design pattern implementation.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *