Design Pattern

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“.

UML class diagram for the Proxy Design Pattern

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

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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago