Agent mode-virtual agent (structured)

In the previous chapter, we learned about remote agents. This chapter will introduce virtual agents.

There are three types of agents:

1. Remote agent, help us control access to remote objects:

The remote agent can act as a local representative of objects on another JVM. The method of calling the agent will be forwarded to the remote execution by the agent through the network, and the result will be returned to the agent through the network, and the agent will transfer the result to the client.

2. Virtual agent, help us control access to create expensive resources

As a representative of creating expensive objects, virtual agents often do not create an object until we actually need it. When the object is before and during creation, the virtual agent will act as the object's substitute. After the object is created, the proxy delegates the request directly to the object.

3. Protect the agent and control access to resources based on permissions.

 

achieve

We will create an  Image  interface and  an entity class that implements the  Image interface. ProxyImage  is a proxy class that reduces   the memory footprint of RealImage object loading.

ProxyPatternDemo , our demo class uses  ProxyImage  to get the Image  object to be loaded  and displays it as required.

UML diagram of proxy mode

1. Create a common interface between the proxy class and the real class

package proxy.virtual;

/**
 * 创建代理类和真正的类的共同接口
 */
public interface Image
{
   void display();
}

2. Create proxy class and real class

package proxy.virtual;

/**
 * 图片代理类
 */
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();
   }
}
package proxy.virtual;

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

3. Start the test

package proxy.virtual;

/**
 * 客户端模拟类
 * 当用户想要访问图片的时候,先交给代理类处理,如果图片已经创建,则直接取出图片,否则在从硬盘上下载图片,所以当图片存在时,
 * 则不需要再次创建RealImage 对象从硬盘上下载图片,避免大量创建 RealImage 对象,消耗内存
 */
public class Client {
   
   public static void main(String[] args) {
      Image image = new ProxyImage("timg.gif");
 
      // 图像将从磁盘加载
      image.display(); 
      System.out.println("");
      // 图像不需要从磁盘加载
      image.display();  
   }
}
Loading timg.gif
Displaying timg.gif

Displaying timg.gif

 As can be seen from the above test interface, when the user wants to access the picture, it is first handed over to the agent class for processing. If the picture has been created, the picture is directly taken out, otherwise the picture is downloaded from the hard disk, so when the picture exists, No need to create a RealImage  object again to download pictures from the hard disk, to avoid creating a lot of  RealImage  objects and consume memory

Published 138 original articles · praised 34 · 150,000 views

Guess you like

Origin blog.csdn.net/bbj12345678/article/details/105266960