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.
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.
Let’s see the following diagram of the Proxy patterns and its component classes.
also read:
Let’s see the following benefits of the Proxy Design Patterns.
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.
/** * */ package com.doj.patterns.structural.proxy; /** * @author Dinesh.Rajput * */ public interface Shape { void draw(); }
/** * */ 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); } }
/** * */ 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(); } }
/** * */ 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(); } }
Drawing Shape Cricle
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…