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
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
also read:
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
Thanks Jonny. Most of the footage is ARRI and we discovered the LUT was adding some weight to the project, but it only went down 6MB after we removed it. I did a test bringing in just the RAW footage and after attaching the proxies it doubled in size. This is a feature documentary and we have a lot of footage and a very tight schedule, so I’m worried the proxy workflow might slow us down in the future and I have started a new project just with the proxies. I would love not to loose the ability to connect to the raw footage, but thinking my project might slow me down after we start the edit worries me. The project with just the proxy files is only 6MB, while the project with the RAW media attached to proxies is 26MB. I know that 26MB might not be that big of a deal, but that is before creating any sequences.