Proxy pattern

In the Proxy Pattern, one class represents the functionality of another class. This type of design pattern is a structural pattern.

The proxy pattern controls access to the original object by introducing a proxy object. The proxy object acts as an intermediary between the client and the target object, responsible for forwarding the client's requests to the target object, and can perform additional processing before and after forwarding the request.

In the proxy pattern, we create an object that has an existing object in order to provide a functional interface to the outside world.

Introduction

intent

Provide a proxy for another object to control access to this object.

Main Problems Solved

  • The proxy pattern solves problems that may arise when directly accessing certain objects, such as high object creation cost, need for security control, or remote access.

Use Case

  • When it is necessary to perform some control or additional processing when accessing an object.

Implementation Approach

  • Add an Intermediate Layer: Create a proxy class as an intermediate layer for the real object.
  • Proxy and real object composition: The proxy class holds a reference to the real object and controls access when accessing it.

Key code

  • Proxy class: Implement the same interface as the real object and add additional control logic.
  • Real Object: the object that actually performs the task.

Application example

  • Shortcut: Shortcuts in the Windows system act as proxies for files or programs.
  • Role Play: Sun Wukong acts as a proxy for Gao Cuilan, and Zhu Bajie cannot distinguish between them.
  • Sales outlet: When purchasing train tickets, the ticket agency acts as a proxy for the railway station.
  • check: As a proxy for bank account funds, controlling access to the funds.
  • Spring AOP: Use the proxy pattern to implement aspect-oriented programming.

Advantages

  • Separation of Concerns: The proxy pattern separates access control from business logic.
  • Extensibility: It is flexible to add additional functionality or control.
  • Intelligent: It can intelligently handle access requests, such as lazy loading, caching, etc.

Disadvantages

  • Performance overhead: Adding a proxy layer may affect the processing speed of requests.
  • Implementation Complexity: Some types of proxy patterns may be more complex to implement.

Usage suggestions

  • Choose the appropriate proxy type according to specific needs, such as remote proxy, virtual proxy, protection proxy, etc.
  • Ensure that the proxy class interface is consistent with the real object so that clients can use the proxy transparently.

Notes

  • Difference from the Adapter pattern: The adapter pattern changes the interface, while the proxy pattern does not change the interface.
  • Difference from the Decorator pattern: The decorator pattern is used to enhance functionality, while the proxy pattern is used to control access.

Structure

It mainly involves the following core roles:

  • Abstract Subject:

    • Defines a common interface for the real subject and the proxy subject, so that the proxy subject can be used anywhere the real subject is used.
  • Real Subject:

    • Implements the abstract subject interface and is the real object represented by the proxy object. The client directly accesses the real subject, but in some cases it can be accessed indirectly through the proxy subject.
  • Proxy:

    • Implements the abstract subject interface and holds a reference to the real subject. The proxy subject usually provides some additional functionality on top of the real subject, such as lazy loading, permission control, logging, etc.
  • Client:

    • Uses the abstract subject interface to operate on the real subject or the proxy subject, without needing to know which specific implementation class it is.

Implementation

We will create anImageinterface and implementsImagethe interface's entity class.ProxyImageis a proxy class, reducingRealImagethe memory footprint of object loading.

ProxyPatternDemoClass usageProxyImageto obtain the to-be-loadedImagethe object, and displays it as required.

代理模式的 UML 图

Step 1

Create an interface.

Image.java

public interface Image { void display(); }

Step 2

Create an entity class that implements the interface.

RealImage.java

public class RealImage implements Image { private String fileName; public RealImage(String fileName){ this.fileName = fileName; loadFromDisk(fileName); } @Override public void display() { System.out.println("Displaying " + fileName); } private void loadFromDisk(String fileName){ System.out.println("Loading " + fileName); } }

ProxyImage.java

public class ProxyImage implements Image{ private RealImage realImage; private String fileName; public ProxyImage(String fileName){ this.fileName = fileName; } @Override public void display() { if(realImage == null){ realImage = new RealImage(fileName); } realImage.display(); } }

Step 3

When requested, useProxyImageTo obtainRealImageObject of the class.

ProxyPatternDemo.java

public class ProxyPatternDemo { public static void main(String[] args) { Image image = new ProxyImage("test_10mb.jpg"); //The image will be loaded from disk image.display(); System.out.println(""); //Images do not need to be loaded from disk image.display(); } }

Step 4

Execute the program, and output the result:

Loading test_10mb.jpg
Displaying test_10mb.jpg

Displaying test_10mb.jpg
other extensions