Small Talk on Design Patterns (8)—Agency Pattern

Column introduction

Column address

link

Column introduction

It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.
Insert image description here

proxy mode

The proxy pattern is a structural design pattern that allows you to control access to other objects by creating a proxy object. The proxy object serves as the interface of the proxy object. The client accesses the proxy object through the proxy object, thus achieving indirect access to the proxy object.

Agent model role analysis

Abstract subject

The common interface between the proxy object and the proxied object is defined, and the client accesses the proxied object through the abstract theme.

Real Subject

It implements the abstract subject interface and is a proxy object, and the proxy object will access it indirectly.

Proxy

It implements the abstract topic interface and also contains a reference to the real topic. The client accesses the real topic through the proxy object.
Insert image description here

Application scenarios

remote agent

The proxy mode is often used in network communications, such as remote method invocation (RPC). In a distributed system, the client can call methods on the remote server through the proxy object. The proxy object is responsible for sending the call request to the remote server and returning the result. The remote proxy hides the details of the underlying network communication, allowing the client to call remote methods just like calling local methods.

virtual agent

Virtual proxies are used to perform some additional processing when accessing objects. A common example is lazy loading (Lazy Loading). When the creation or loading of an object is very resource-intensive, a virtual proxy can be used to postpone the creation or loading of the object until the object is actually needed to be accessed. For example, you can use a virtual proxy to lazily load image data when an image is loading, and the image data is only actually loaded when the image needs to be displayed.

security agent

Security agents are used to control access to objects. For example, in a rights management system, a security proxy can be used to restrict access to an object to only users with specific roles. The proxy object can check the user's role before accessing the real object. If the user has access rights, access to the real object is allowed, otherwise access is denied.

Insert image description here

Intelligent citation proxy

Smart reference proxies are used to add some extra functionality when accessing objects. A common example is the caching function. The proxy object can check whether the corresponding result exists in the cache before accessing the real object. If it exists, it directly returns the cached result to avoid repeated calculations. In addition, you can also use smart reference proxies to implement object pools. Proxy objects can manage the creation and destruction of objects, and obtain objects from the object pool when accessing objects.

Summarize

The proxy pattern is widely used in many practical applications. By introducing proxy objects, indirect access to the proxy object can be achieved, and some additional processing can be done before and after access, such as network communication, delayed loading, permission control, and function expansion. The proxy mode can improve the flexibility and scalability of the system, but it also needs to trade off the complexity and performance of the system.
Insert image description here

Java program implementation

First, we define an interface Image, which represents the interface of the image object:

public interface Image {
    
    
    void display();
}

Secondly, we create a real image class RealImage, implement the Image interface, and represent the real image object:

public class RealImage implements Image {
    
    
    private String filename;

    public RealImage(String filename) {
    
    
        this.filename = filename;
        loadFromDisk();
    }

    private void loadFromDisk() {
    
    
        System.out.println("Loading image from disk: " + filename);
    }

    public void display() {
    
    
        System.out.println("Displaying image: " + filename);
    }
}

Then, we create a proxy class ProxyImage, implement the Image interface, and represent the proxy object of the image:

public class ProxyImage implements Image {
    
    
    private String filename;
    private RealImage realImage;

    public ProxyImage(String filename) {
    
    
        this.filename = filename;
    }

    public void display() {
    
    
        if (realImage == null) {
    
    
            realImage = new RealImage(filename);
        }
        realImage.display();
    }
}

Finally, we can use a proxy object to access the real image object, for example:

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

        // 第一次访问,会创建真实的图像对象并显示
        image.display();

        // 第二次访问,直接显示之前创建的真实图像对象
        image.display();
    }
}

Output results

Loading image from disk: test.jpg
Displaying image: test.jpg
Displaying image: test.jpg

program analysis

In the above example, the ProxyImage class acts as a proxy object that does some extra processing before accessing the real image object. When the image is accessed for the first time, the proxy object will create a real image object and display it; when the image is accessed for the second time, the proxy object directly displays the real image object created before, avoiding repeated loading and display. Through proxy objects, we can achieve indirect access to real objects and do some additional processing before and after access.
Insert image description here

Advantages and Disadvantages Analysis

advantage

The proxy mode can achieve indirect access to real objects, and can do some additional processing before and after access, such as permission control, delayed loading, caching, etc.
Proxy objects can hide the specific implementation details of real objects and protect the security of real objects.
The proxy mode can improve the flexibility and scalability of the system, and new proxy objects can be added without modifying the real objects.
The proxy pattern conforms to the single responsibility principle and can separate the real object and the proxy object, respectively responsible for their respective functions.

shortcoming

The introduction of proxy objects will increase the complexity of the system, increase the number of codes and the difficulty of maintenance.
The proxy pattern introduces additional overhead because the real object needs to be accessed through the proxy object, which may lead to performance degradation.
If the creation and destruction process of proxy objects is complicated, it may affect the performance of the system.
Insert image description here

Summarize

The proxy pattern is widely used in many real-world applications to provide additional functionality and security of real objects. However, when using the proxy mode, you need to weigh the complexity and performance of the system to ensure that the creation and destruction process of proxy objects does not affect the performance of the system.

Guess you like

Origin blog.csdn.net/weixin_74888502/article/details/133456597