1, what is the proxy mode:
Provides an alias or placeholder for another object to access the object.
2. What are the benefits of the agent model: (1) Delayed loading
When you need to view a large picture from the network, you can use the proxy mode to see its thumbnail to see if it is the image you want.
(2) Control of Access
The proxy mode provides a representation of another object to control the client's access to the object.
For example, when users need to view some important files, you can use the proxy mode to first check if the user has this permission.
(3) reduce coupling, increase flexibility and scalability of the system
Because the user holds a reference to the proxy object that is not the object that executes the user request, it implements the full decoupling of the caller from the callee!
(4) ...
3, the structure of the proxy mode:
UML:
:
Why do proxy objects and real objects implement the same interface?
The implementation of the same interface can be guaranteed to use the proxy object as long as the real object can be used in place!
4.role in proxy mode: (1) Agent object (proxy)
Save a reference so that the agent can access the entity;
Implement the interface with Subject so that the agent can use the proxy object where the real object is used;
Controls the access of the object and may be responsible for creating and deleting it.
(2) Theme interface (Subject):
Define A common interface between realsubject and proxies, so that you can use proxies wherever you use realsubject .
(3) Real objects (realsubject);
Defines the object to be proxied by proxy.
5, application examples:
Now Xiao Wang students want to buy a train ticket home, so he came to the train ticket consignment point to buy tickets. To the point of sale he told Aunt he wanted to buy a train ticket home, Aunt Smooth help Xiao Wang bought the ticket home.
Now let's analyze the process of Aunt help Xiao Wang to buy tickets: In fact, the sale point is not to sell tickets, the real ticket is the railway station.
Process:
1 Package Com.tony.proxy; 2 3 /** 4 * Subject:proxy and Realsubject must implement this interface 5 * ticketing operation available 6 */ 7 Public Interface Subject {8 void soldtickets (); 9 }
1 PackageCom.tony.proxy;2 /**3 * 4 * Point of Sale (Proxy): holding a reference to a real object5 */6 Public classProxyImplementsSubject {7 8 PrivateSubject Realsubject;9 Ten PublicProxy () { OneRealsubject =NewRealsubject (); A } - - @Override the Public voidsoldtickets () { -System.out.println ("Consignment point: There are customers need to buy train tickets ..."); - realsubject.soldtickets (); -System.out.println ("Consignment point: Ticket is being issued ..."); + } - +}
1 PackageCom.tony.proxy;2 3 /**4 * 5 * Train station (Realsubject): The object that really handles the request6 */7 Public classRealsubjectImplementsSubject {8 9 @OverrideTen Public voidsoldtickets () { OneSystem.out.println ("railway station: Orders received, tickets allowed ..."); A } - -}
1 PackageCom.tony.proxy;2 3 Public classClient {4 5 PrivateSubject proxy;6 7 PublicClient () {8Proxy =NewProxy ();9 }Ten One Public voidbuytickets () { ASystem.out.println ("Xiao Wang: I want to buy a ticket ..."); - proxy.soldtickets (); -System.out.println ("Xiao Wang: has bought a ticket ..."); the } - -}
1 Package Com.tony.proxy; 2 3 Public class Test {4 Public Static void Main (string[] args) {5 New Client (); 6 c.buytickets (); 7 }8 }
Because the train station ticketing system is very important, can not allow everyone to operate directly to it, so the railway station to get a safe agent (consignment point). Want to buy a ticket, no problem, go to my agent where I bought not directly to you tickets. This guarantees the security of the ticketing system to some extent. This is also one of the benefits of using proxy mode: Access is controlled and only open to objects that have permissions.
6, the difference between the proxy mode and other modes:(1) The difference from the decorator pattern:
You may find that the UML diagram of the two modes is almost identical, does it mean that the two models can be substituted for each other?
No! The purpose of the two modes is different:
The decorator's purpose is to add new functionality to the object, while the agent Controls access to an Object!
(2) The difference from the adapter mode:
Both the proxy mode and the adapter mode are blocked in front of other objects and are responsible for forwarding requests to them.
But they are structurally different: The adapter alters the interface to which the object is adapted, and the proxy implements the same interface.
7, the disadvantages of Agent mode:
(1) Adding a proxy object to the client and target object will slow down the request processing and affect the system performance.
(2) Increase the complexity of the system. As with other Packers (wrapper), proxies can cause the number of classes in your design to increase.
8. Summary:
Although the proxy mode can affect performance, it will give us a lot of benefits: it can access control of an object, improve the security of the system, it can be the caller and the callee to achieve complete decoupling, improve the system's elasticity. In programming, we have to weigh the system performance and program security, scalability, and so on: to design a good program, sacrificing some performance is also worthwhile!
The application of proxy mode is very extensive, and almost all the excellent open source frameworks are used: Spring, MyBatis, Struts2 and so on.
Therefore, in order to design a good program must master the proxy mode.
There are many variants of the proxy pattern, such as: Cache proxy, synchronization agent, firewall proxy and write-on replication agent, smart Reference agent, complex hidden agent.
A branch of proxy mode-dynamic proxies are often used in our usual programming because it gives us the flexibility of the program. In the next article I will explain the dynamic agent in detail.
Reference documents:
"Head First design mode"
"Design Mode"
Java design mode-proxy mode