Agent model for Java design pattern learning
Proxy mode: provides a proxy for other objects to control access to this object.
Agent mode structure diagram
Simulation demand: Xiao Zhang of class 3 likes Xiao Hong of class 1, but he doesn't know Xiao Hong. He entrusts Xiao Ming of class 1 to give Xiao Hong a gift.
1: Create an interface that both Xiao Zhang and Xiao Ming can implement.
2: Realize small Zhang to give gifts.
3: Realize Xiao Ming's gift from Xiao Zhang and transfer it to Xiao Hong.
4: Test class:
5: Console
Send little red dolls, little red flowers, little red chocolate
Summary:
Applicable scenarios of agent mode
1: Remote proxy is to provide local representation of an object in different address spaces. This can hide the fact that an object exists in different address spaces.
2: Virtual agent is to create expensive objects as needed. It is used to store real objects that take a long time to instantiate.
3: Security agent, which is used to control the access rights of real objects.
4: Intelligent guidance means that when a real object is called, the agent handles something else.
advantage:
1: The agent model can coordinate the caller and callee, and reduce the coupling degree of the system to a certain extent;
2: The proxy object can act as an intermediary between the client and the target object, which protects the target object.
Disadvantages:
1: Due to the addition of proxy objects between the client and the real object, some types of proxy modes may slow down the processing speed of requests;
2: Implementing the proxy pattern requires additional work, and the implementation of some proxy patterns is very complex.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.