Design pattern - agent pattern
Definition: provides a proxy for other objects to control access to this object. In some cases, one object is not suitable or cannot directly reference another object, and the proxy object can act as an intermediary between the customer and the target object.
The class diagram of agent mode is as follows
Interface Abstract role
Declare business methods implemented by real roles through interfaces or abstract classes
RealObject specific roles
It is also called delegated role and delegated role. It is the specific executor of business logic
Proxy role
It is also called delegate class and agent class. It is responsible for the application of the real role, delegating the method limits defined by all abstract topic classes to the implementation of the real topic role, and doing pre-processing and post-treatment before and after the processing of the real topic role, so as to implement the business logic to be called by the agent role
Proxy mode application scenario
(1) Remote proxy: provide a local representative object for an object located in different address spaces. This different address space can be in this computer or in another computer.
(2) Virtual agent: create a resource consuming object as needed, so that the object will be created only when needed.
(3) Firewall: protect the target from malicious users
(4) Intelligent reference agent: when an object is referenced, it provides some additional operations, such as recording the number of times the object is accessed.
Take the details of each file in the folder as an example
When we preview which files are saved in the folder, we only need to view the necessary information of the file (file display icon, file name, file type, creation date, resource size, etc.). The computer only needs to load some of the information we view into the memory. When we decide to open a file, the computer will load the file into the memory.
The code is as follows
Interface Abstract role
// File interface public interface IFile { // file name string FileName(); // Modify file name void SetFileName(string name); // File type, extension string ExtensionName(); // date string Date(); // Resource size int Size(); }
RealObject specific roles
public class MoveFile : IFile { private string _fileName; private string _extensionName; private string _date; private int _size; public MoveFile(string fileName, string extensionName, string date, int size) { _fileName = fileName; _extensionName = extensionName; _date = date; _size = size; } // file name public string FileName() { return _fileName; } // Modify file name public void SetFileName(string name) { _fileName = name; } // File type, extension public string ExtensionName() { return _extensionName; } // date public string Date() { return _date; } // Resource size public int Size() { return _size; } }
Proxy role
public class FileProxy { private IFile _file; public FileProxy() { } public void SetFile(IFile file) { _file = file; } // file name public string FileName() { return _file.FileName(); } // Modify file name public void SetFileName(string name) { _file.SetFileName(name); } // File type, extension public string ExtensionName() { return _file.ExtensionName(); } // Set extension public void SetExtension(string name) { _file.SetFileName(name); } // date public string Date() { return _file.Date(); } // Resource size public int Size() { return _file.Size(); } }
Proxy role
public class FileProxy : IFile { private IFile _file; public FileProxy() { } public void SetFile(IFile file) { _file = file; } // file name public string FileName() { return _file.FileName(); } // Modify file name public void SetFileName(string name) { _file.SetFileName(name); } public string ExtensionName() { return _file.ExtensionName(); } // date public string Date() { return _file.Date(); } // Resource size public int Size() { return _file.Size(); } }
Call as follows
MoveFile moveFile = new MoveFile("A", "mp4", "2020/10/20/18:20", 10000); FileProxy fileProxy = new FileProxy(); fileProxy.SetFile(moveFile); string fileName = fileProxy.FileName(); string extension = fileProxy.ExtensionName(); string date = fileProxy.Date(); int size = fileProxy.Size(); fileProxy.SetFileName("B");
advantage:
(1) Clear responsibilities
The real role is to realize the actual business logic, do not care about other non responsibility transactions, through the later agent
When a transaction is completed, the incidental result is that the programming is concise and clear.
(2) High scalability
The specific theme role will change at any time. As long as it implements the interface, no matter how it changes, it can't escape
If the palm of Buddha (Interface), our proxy class can be used without any modification.
shortcoming
(1) Low efficiency
Due to the addition of proxy objects between clients and specific roles, some types of proxy patterns may slow down the processing of requests
(2) Increase workload
Implementing the proxy pattern requires additional work, and the implementation of some proxy patterns is very complex