Video This is not same as Bridge Pattern.
“Provide a surrogate or placeholder for another object to control access to it” is the intent provided by GoF.
Allows for object level access control by acting as a pass through entity or a placeholder object.

When to use this pattern?
Proxy pattern is used when we need to create a wrapper to cover the main object’s complexity from the client.
Types of proxies (3 intent of Proxy Pattern)
Remote proxy:
They are responsible for representing the object located remotely. Talking to the real object might involve marshalling and unmarshalling of data and talking to the remote object. All that logic is encapsulated in these proxies and the client application need not worry about them.
Virtual proxy:
These proxies will provide some default and instant results if the real object is supposed to take some time to produce results. These proxies initiate the operation on real objects and provide a default result to the application. Once the real object is done, these proxies push the actual data to the client where it has provided dummy data earlier.
Protection proxy:
If an application does not have access to some resource then such proxies will talk to the objects in applications that have access to that resource and then get the result back.
Smart Proxy:
A smart proxy provides additional layer of security by interposing specific actions when the object is accessed. An example can be to check if the real object is locked before it is accessed to ensure that no other object can change it.
Benefits:
- One of the advantages of Proxy pattern is security.
- This pattern avoids duplication of objects which might be huge size and memory intensive. This in turn increases the performance of the application.
- The remote proxy also ensures about security by installing the local code proxy (stub) in the client machine and then accessing the server with help of the remote code.
Drawbacks/Consequences:
This pattern introduces another layer of abstraction which sometimes may be an issue if the RealSubject code is accessed by some of the clients directly and some of them might access the Proxy classes. This might cause disparate behaviour.
- A proxy may hide information about the real object to the client.
- A proxy may perform optimization like on demand loading.
- A proxy may do additional house-keeping job like audit tasks.
- Proxy design pattern is also known as surrogate design pattern.
So How Does It Work In Java?
Let’s continue with the idea of using a proxy for loading images. First, we should create a common interface for the real and proxy implementations to use:
public interface Image{
public void displayImage();
}
The RealImage implementation of this interface works as you’d expect:
public class RealImage implements Image{ public RealImage(URL url) { //load up the image loadImage(url); } public void displayImage() { //display the image } //a method that only the real image has private void loadImage(URL url) { //do resource intensive operation to load image }}
Now the Proxy implementation can be written, which provides access to the RealImage class. Note that it’s only when we call the displayImage() method that it actually uses the RealImage. Until then, we don’t need the data.
public class ProxyImage implements Image{ private URL url; public ProxyImage(URL url) { this.url = url; } //this method delegates to the real image public void displayImage() { RealImage real = new RealImage(url); real.displayImage(); }}
And it’s really as simple as that. As far as the client is concerned, they will just deal with the interface.
Adapter design pattern provides a different interface from the real object and enables the client to use it to interact with the real object. But, proxy design pattern provides the same interface as in the real object.
Decorator design pattern adds behaviour at runtime to the real object. But, Proxy does not change the behaviour instead it controls the behaviour.
Link Real world example
Have you ever used an access card to go through a door? There are multiple options to open that door i.e. it can be opened either using access card or by pressing a button that bypasses the security. The door’s main functionality is to open but there is a proxy added on top of it to add some functionality. Let me better explain it using the code example below.
In plain words
Using the proxy pattern, a class represents the functionality of another class.
Wikipedia says
A proxy, in its most general form, is a class functioning as an interface to something else. A proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked.
Programmatic Example
Taking our security door example from above. Firstly we have the door interface and an implementation of door
interface Door { public function open(); public function close(); } class LabDoor implements Door { public function open() { echo "Opening lab door"; } public function close() { echo "Closing the lab door"; } } Then we have a proxy to secure any doors that we want
class SecuredDoor { protected $door; public function __construct(Door $door) { $this->door = $door; } public function open($password) { if ($this->authenticate($password)) { $this->door->open(); } else { echo "Big no! It ain't possible."; } } public function authenticate($password) { return $password === '$ecr@t'; } public function close() { $this->door->close(); } } And here is how it can be used
$door = new SecuredDoor(new LabDoor()); $door->open('invalid'); // Big no! It ain't possible. $door->open('$ecr@t'); // Opening lab door $door->close(); // Closing lab door
[…] Proxy Link […]
LikeLike