proxy mode
In the Proxy Pattern, one class represents the functionality of another class. This type of design pattern is a structural pattern.
In proxy pattern, we create objects with existing objects in order to provide functional interfaces to the outside world.
Introduction
Intent: Provide a proxy for other objects to control access to this object.
Mainly solves: Problems caused when directly accessing objects, for example: the object to be accessed is on a remote machine. In object-oriented systems, direct access to some objects will cause a lot of trouble to users or the system structure due to certain reasons (for example, object creation is expensive, or certain operations require security control, or require out-of-process access). We can add an access layer to this object when accessing this object.
When to use: Want to have some control when accessing a class.
How to solve: Add an intermediate layer.
Key code: Implementation and proxy class combination.
Application examples: 1. Shortcuts in Windows. 2. When Zhu Bajie went to find Gao Cuilan, he turned out to be Sun Wukong. It can be understood this way: Gao Cuilan's appearance was abstracted, and both Gao Cuilan himself and Sun Wukong implemented this interface. When Zhu Bajie visited Gao Cuilan, he couldn't tell that this was Sun Wukong, so Sun Wukong was said to be Gao Cuilan. Agent class. 3. You don’t have to buy train tickets at the train station, you can also go to an agency. 4. A check or bank deposit receipt is a proxy for the funds in the account. Checks are used in place of cash in market transactions and provide control over funds held in the issuer's account. 5. spring aop.
Advantages: 1. Clear responsibilities. 2. High scalability. 3. Intelligent.
Disadvantages: 1. Since a proxy object is added between the client and the real topic, some types of proxy modes may cause the request processing speed to slow down. 2. Implementing the proxy mode requires additional work, and the implementation of some proxy modes is very complex.
Usage scenarios: Divided according to responsibilities, there are usually the following usage scenarios: 1. Remote agent. 2. Virtual agent. 3. Copy-on-Write agent. 4. Protect (Protect or Access) agent. 5. Cache agent. 6. Firewall proxy. 7. Synchronization agent. 8. Smart Reference agent.
Notes: 1. The difference between the adapter pattern and the adapter pattern: the adapter pattern mainly changes the interface of the object under consideration, while the proxy pattern cannot change the interface of the proxy class. 2. The difference between the decorator mode and the decorator mode: the decorator mode is to enhance the function, while the proxy mode is to control it.
Implementation
We will create a 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 obtain the Image object to be loaded and display it as required.
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 obtain an object of class RealImage.
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