<p>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 <strong><a href="https://dineshonjava.com/design-patterns_25/">23 GOF design patterns</a></strong>.</p>
<h2><strong>The Proxy Pattern</strong></h2>
<div style="text-align: left;">
<blockquote><p><strong>According to the Gang of Four:</strong></p>
<div style="text-align: left;">Provide a surrogate or placeholder for another object to control access to it.</div>
</blockquote>
</div>
<p>The intent of this design pattern to provide a different class for an another class with its functionality to outer world.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Pattern</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern" target="_blank" rel="noopener"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns" target="_blank" rel="noopener"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput" target="_blank" rel="noopener"><img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern-243x300.jpg" width="243" height="300" /></a></div>
</div>
<h2><strong>UML class diagram for the Proxy Design Pattern</strong></h2>
<p>Let&#8217;s see the following diagram of the Proxy patterns and its component classes.</p>
<p><img class="aligncenter wp-image-3583 size-full" src="https://dineshonjava.com/wp-content/uploads/2017/12/proxy.gif" alt="Proxy Pattern Design Patterns in Java" width="405" height="252" /></p>
<h3>Proxy</h3>
<ul>
<li>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.</li>
<li>It provides an interface identical to Subject&#8217;s so that a proxy can be substituted for for the real subject.</li>
</ul>
<h3>Subject</h3>
<ul>
<li>It defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.</li>
</ul>
<h3>RealSubject</h3>
<ul>
<li>It defines the real object that the proxy represents</li>
</ul>
<div style="text-align: left;">
<blockquote><p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/adapter-design-pattern/" href="https://dineshonjava.com/adapter-design-pattern/">Adapter Design Pattern</a></li>
<li><a title="https://dineshonjava.com/bridge-design-patterns/" href="https://dineshonjava.com/bridge-design-patterns/">Bridge Design Pattern</a></li>
<li><a title="https://dineshonjava.com/decorator-design-pattern/" href="https://dineshonjava.com/decorator-design-pattern/">Decorator Design Pattern</a></li>
<li><a title="https://dineshonjava.com/facade-design-pattern/" href="https://dineshonjava.com/facade-design-pattern/">Facade Design Pattern</a></li>
<li><a title="https://dineshonjava.com/builder-design-pattern/" href="https://dineshonjava.com/builder-design-pattern/">Builder Design Pattern</a></li>
<li><a title="https://dineshonjava.com/flyweight-pattern-design-patterns-java/" href="https://dineshonjava.com/flyweight-pattern-design-patterns-java/">Flyweight Design Pattern</a></li>
<li><a title="https://dineshonjava.com/creational-design-patterns/" href="https://dineshonjava.com/creational-design-patterns//">Creational GoF Design Patterns</a></li>
<li><a title="https://dineshonjava.com/behavioral-design-pattern/" href="https://dineshonjava.com/behavioral-design-pattern/">Behavioral GoF Design Patterns</a></li>
</ul>
</blockquote>
</div>
<h2><strong>Benefits of Proxy Pattern</strong></h2>
<p>Let&#8217;s see the following benefits of the Proxy Design Patterns.</p>
<ul>
<li>This pattern hides the actual object from the outside world.</li>
<li>It can improve the performance because it is creating an object on demand.</li>
</ul>
<h2><strong>Sample Implementation of the Proxy Design Pattern</strong></h2>
<p>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.</p>
<h3>Step 1: Create an interface.<br />
Shape.java</h3>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.structural.proxy; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public interface Shape { 
	void draw(); 
} 
 
</pre>
<h3>Step 2: Create concrete classes implementing the same interface.</h3>
<h3>RealShape.java</h3>
<pre class="highlight">/** 
 * 
 */ 
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); 
	} 
 
} 
 
</pre>
<h3>ProxyShape.java</h3>
<pre class="highlight">/** 
 * 
 */ 
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(); 
	} 
 
} 
 
</pre>
<h3>Step 3: Use the ProxyShape to get object of RealShape class when required.<br />
ProxyPatternDemo.java</h3>
<pre class="highlight">/** 
 * 
 */ 
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(); 
	} 
 
} 
 
</pre>
<h3>Step 4: Let&#8217;s run this demo class and verify the output.</h3>
<pre class="highlight">Drawing Shape Cricle 
 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/flyweight-pattern-design-patterns-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/structural-design-patterns/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '3582'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…