Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Agent mode


May 27, 2021 Design mode


Table of contents


Agent mode

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

In proxy mode, we create objects with existing objects to provide a functional interface to the outside world.

Introduced

Intent: Provides a proxy for other objects to control access to that object.

Key solution: Problems that come with direct access to objects, such as objects to be accessed on remote machines. In object-oriented systems, some objects can cause a lot of trouble for consumers or system structures for some reason (such as the high cost of object creation, or some operations require security control, or out-of-process access), and we can add an access layer to this object when accessing this object.

When to use: You want to do some control when accessing a class.

How to solve: Add the middle tier.

Key code: Implementation combined with proxy class.

Examples of apps: 1, shortcuts inside Windows. 2 , pig eight ring to find Gao Cuilan results in Sun Wukong change, you can understand this: the appearance of Gao Cuilan abstracted out, Gao Cuilan himself and Sun Wukong have realized this interface, pig eight ring visit Gao Cuilan when you can not see this is Sun Wukong, so Sun Wukong is Gao Cuilan agent class. 3 , buy train tickets do not necessarily buy at the railway station, you can also go to the point of sale. 4 . A check or bank deposit slip is the agent of the funds in the account. C heques are used in place of cash in market transactions and provide control over the funds on the issuer's account. 5 、spring aop。

Pros: 1, clear responsibilities. 2 , high scalability. 3 , intelligent.

Cons: 1, because proxy objects are added between the client and the real topic, some types of proxy patterns can slow down the processing of requests. 2 , the implementation of agent mode requires additional work, some agent mode implementation is very complex.

Use Scenarios: By responsibility, there are usually the following usage scenarios: 1, Remote Agent. 2 , virtual agent. 3 , Copy-on-Write agent. 4 , protection (Protection or Access) agent. 5 , Cache agent. 6 , firewall agent. 7 , synchronization (Synchronization) agent. 8 , Smart Reference agent.

Note: 1, and the difference between the adapter mode: the adapter mode primarily changes the interface of the object under consideration, while the proxy mode does not change the interface of the proxy class. 2 , and the difference between decorator mode: decorator mode in order to enhance functionality, and agent mode is to control.

Realize

We'll create an Image interface and the entity class that implements the Image interface. ProxyImage is a proxy class that reduces the memory footprint loaded by RealImage objects.

ProxyPatternDemo, our demo class uses ProxyImage to get the Image object to load and display it as needed.

Agent mode

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, use ProxyImage to get the objects of the RealImage class.

ProxyPatternDemo.java

public class ProxyPatternDemo {
  
   public static void main(String[] args) {
      Image image = new ProxyImage("test_10mb.jpg");

      //图像将从磁盘加载
      image.display(); 
      System.out.println("");
      //图像将无法从磁盘加载
      image.display();     
   }
}

Step 4

Verify the output.

Loading test_10mb.jpg
Displaying test_10mb.jpg

Displaying test_10mb.jpg