Location>code7788 >text

agency model

Popularity:157 ℃/2024-08-19 08:59:17

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 and is responsible for forwarding the client's request to the target object, while additional processing can be performed before and after the forwarded request.

In the proxy pattern, we create objects with existing objects in order to provide functional interfaces to the outside world.

present (sb for a job etc)

schematic diagram

Provides a proxy for other objects to control access to this object.

Main issues addressed

  • The proxy pattern addresses problems that may be encountered when accessing certain objects directly, such as the high cost of object creation, the need for security controls, or remote access.

Usage Scenarios

  • When some control or additional processing is required when accessing an object.

implementation method

  • Adding an intermediate layer: Create a proxy class that acts as an intermediate layer for real objects.
  • Proxy and Real Object Combination: The proxy class holds a reference to the real object and controls it when accessed.

keycode

  • agent class: Implement the same interfaces as real objects and add additional control logic.
  • real object: The object that actually performs the task.

Examples of applications

  • (computer) shortcut: A shortcut on a Windows system that acts as a proxy for a file or program.
  • role-playing (as a game of chess): Sun Wukong acts as an agent for Gautreaux, and InuYasha is indistinguishable.
  • outlet: When purchasing train tickets, the agency acts as an agent for the train station.
  • cheques: Acts as an agent for funds in a bank account and controls access to the funds.
  • Spring AOP: Use the proxy pattern to implement cutter-oriented programming.

vantage

  • Segregation of duties: The proxy model separates access control from business logic.
  • scalability: Flexibility to add additional features or controls.
  • intellectualize: Access requests can be handled intelligently, such as delayed loading, caching, etc.

drawbacks

  • performance overhead: The addition of a proxy layer may affect the speed of request processing.
  • Realizing complexity: Some types of proxy patterns can be more complex to implement.

Recommendations for use

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

caveat

  • Differences with the Adapter Pattern: Adapter mode changes the interface, while proxy mode does not.
  • Differences from the Decorator Pattern: The decorator pattern is used to enhance functionality and the proxy pattern is used to control access.

framework

The following core roles are primarily involved:

  • Abstract Subject.

    • Defines a common interface for real themes and proxy themes so that proxy themes can be used wherever real themes are used.
  • Real Subject.

    • Implements the Abstract Topic interface, which is the real object represented by the proxy object. Clients access real topics directly, but in some cases can access them indirectly through proxy topics.
  • Proxy.

    • implements the abstract theme interface and holds a reference to the real theme. Proxy topics usually provide some additional functionality on top of the real topic, such as delayed loading, permission control, logging, etc.
  • Client.

    • Use the Abstract Topic Interface to manipulate real topics or proxy topics without needing to know exactly which implementation class.

realization

We will create aImage interface and an implementation of theImage The entity class of the interface.ProxyImage is a proxy class that reduces theRealImage Memory footprint for object loading.

ProxyPatternDemo class usageProxyImage to get theImage objects and display them as required.

代理模式的 UML 图

Step 1

Create an interface.

public interface Image { void display(); }

Step 2

Create entity classes that implement the interface.

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); } }

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, use theProxyImage to getRealImage object of the class.

public class ProxyPatternDemo { public static void main(String[] args) { Image image = new ProxyImage("test_10mb.jpg"); // Images 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 results:

Loading test_10mb.jpg
Displaying test_10mb.jpg

Displaying test_10mb.jpg