Proxy Pattern – Design Patterns in Java

Proxy Pattern provide an object of class that has the functionality of another class with having it. This pattern comes under the structural design pattern of the 23 GOF design patterns.

The Proxy Pattern

According to the Gang of Four:

Provide a surrogate or placeholder for another object to control access to it.

The intent of this design pattern to provide a different class for an another class with its functionality to outer world.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Pattern“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.
Spring-5-Design-Pattern

UML class diagram for the Proxy Design Pattern

Let’s see the following diagram of the Proxy patterns and its component classes.

Proxy Pattern Design Patterns in Java

Proxy

  • It 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.
  • It provides an interface identical to Subject’s so that a proxy can be substituted for for the real subject.

Subject

  • It defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.

RealSubject

  • It defines the real object that the proxy represents

Benefits of Proxy Pattern

Let’s see the following benefits of the Proxy Design Patterns.

  • This pattern hides the actual object from the outside world.
  • It can improve the performance because it is creating an object on demand.

Sample Implementation of the Proxy Design Pattern

We are going to create a Shape interface and concrete classes implementing the Shape interface. ProxyShape is a proxy class to reduce memory footprint of RealShape object loading. ProxyPatternDemo, our demo class, will use ProxyShape to get an Shape object to load and display as it needs.

Step 1: Create an interface.
Shape.java

/**
 * 
 */
package com.doj.patterns.structural.proxy;

/**
 * @author Dinesh.Rajput
 *
 */
public interface Shape {
	void draw();
}

Step 2: Create concrete classes implementing the same interface.

RealShape.java

/**
 * 
 */
package com.doj.patterns.structural.proxy;

/**
 * @author Dinesh.Rajput
 *
 */
public class RealShape implements Shape {
	
	 private String shapeName;
	 
	 public RealShape(String shapeName){
		 this.shapeName = shapeName;
	 }

	 
	@Override
	public void draw() {
		System.out.println("Drawing Shape " + shapeName);
	}

}

ProxyShape.java

/**
 * 
 */
package com.doj.patterns.structural.proxy;

/**
 * @author Dinesh.Rajput
 *
 */
public class ProxyShape implements Shape {
	
	private RealShape realShape;
	private String shapeName;

	public ProxyShape(String shapeName){
		this.shapeName = shapeName;
	}

	@Override
	public void draw() {
		if(realShape == null){
			realShape = new RealShape(shapeName);
		}
		realShape.draw();
	}

}

Step 3: Use the ProxyShape to get object of RealShape class when required.
ProxyPatternDemo.java

/**
 * 
 */
package com.doj.patterns.structural.proxy;

/**
 * @author Dinesh.Rajput
 *
 */
public class ProxyPatternDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Shape shape = new ProxyShape("Cricle");
		//shape going to draw
		shape.draw(); 
	}

}

Step 4: Let’s run this demo class and verify the output.

Drawing Shape Cricle

Previous
Next

One Response

  1. kaka September 3, 2019