Concept:
The proxy mode is the structure pattern of the object. The proxy mode provides a proxy object for an object, and the proxy object controls the reference to the original object.
is one person or institution acting on behalf of another person or institution. In some cases, a client does not want or cannot refer directly to an object, whereas a proxy object can act as an intermediary between the client and the target object.
Class Diagram:
Roles in proxy mode:
Abstract Object role: declares a common interface between the target object and the proxy object, so that the proxy object can be used wherever the target object can be used.
target Object role: defines the target object that the proxy object represents.
proxy Object role: the proxy object contains a reference to the target object, so that the target object can be manipulated at any time, and the proxy object provides the same interface as the target object so that the target object can be substituted at any time. Proxy objects typically perform an operation before or after a client call is passed to the target object, rather than simply passing the call to the target object.
Source
Abstract Object Role
Public Abstract class abstractobject { // operation Public abstractvoid Operation ();}
Target object Role
Public class extends abstractobject { @Override publicvoid operation () { // Some operations SYSTEM.OUT.PRINTLN ("Some operations");} }
Proxy Object Role
Public class extends abstractobject{ new realobject (); @Override publicvoid operation () { // The target object can be invoked before the relevant operation SYSTEM.OUT.PRINTLN ("before"); Realobject.operation (); // The target object can be invoked after the related operation System.out.println ("after");} }
Client
Public class Client { publicstaticvoid main (string[] args) { // TODO auto-generated Method stub New proxyobject (); Obj.operation (); }}
From the above example, you can see that the proxy object delegates the client's call to the target object, and can perform a specific operation before the method of the target object is called.
Application Scenarios
In the real world, the Secretary is the equivalent of an agent, the boss meeting, then inform the staff meeting time, decorate the venue, after the meeting and other meetings related work can be handed to the secretary to do, the boss just need to have a meeting on the line, do not need to do those things personally. Similarly, in our programming we can also use the proxy pattern to decouple the code that is composed of a series of unrelated logic, such as the log code in the business code can be done in the agent. Spring's AOP is a typical dynamic proxy application.
Application form of proxy mode
(1) Remote proxy-You can hide the fact that an object exists in a different address space. Also allows the client to access objects on the remote machine, the remote machine may have better computational performance and processing speed, and can quickly respond to and process client requests. (JAVA RMI)
(2) Virtual proxy – allows objects with large memory overhead to be created when needed. It is only created when we really need this object.
(3) Copy-on-write agent (Copy-on-write proxy) – Used to control the replication of objects by delaying the copying of objects until the customer really needs them. is a variant of the virtual agent.
(4) Protection agent (Protection (access) proxy) – provides different levels of access to target objects for different customers
(5) Cache proxy – provides temporary storage for the result of a costly operation that allows multiple customers to share results to reduce compute or network latency.
(6) Firewall proxy (Firewall proxy) – Control access to network resources and protect topics from malicious customers.
(7) Synchronous agent (Synchronizationproxy) – Provides secure access to topics in multi-threaded situations.
(8) Smart Reference Agent (Smart Referenceproxy)-When an object is referenced, provides some extra action, such as recording the number of times this object is called.
(9) Complex hidden proxy (complexity Hidingproxy) – The complexity of hiding a complex set of classes and access control. Sometimes called the appearance agent (Façade proxy), it is not difficult to understand. Complex hidden proxies and appearance patterns are not the same because the proxy controls access, and the appearance pattern is not the same because the proxy controls access, while the façade mode only provides another set of interfaces.
the difference between agent mode and decorator mode
Proxy mode: Controls the access of the object without changing the interface
Example: Monkey King plays and replaces the high family three miss
Monkey King played Miss Gaojiasan, so it can be said that the Sun Wukong and Miss Gaojiasan have a common interface. If pig only want to see the had bloomed face of the high family, or talk about the sky, then the three Miss "Agent" Monkey King is allowed, but pig want to kiss the mouth, then it is not possible. This is an application that protects the proxy mode. The client's request is passed to the real subject object only if the proxy object is considered appropriate.
Decorative mode: Dynamically extending the function of an object without changing the interface
Sun Wukong has 72 changes, in the eyes of the two lang God, he will always be the Hozen. Decorating mode dynamically attaches more responsibility to an object in a transparent manner to the customer. In other words, the client does not feel that the object is different before and after decorating. Adornment mode expands the functionality of an object without using the creation of more subclasses. Each of his changes brought him an additional skill. When he became a fish, he could swim in the water, and when he became a sparrow, he could fly in the sky. And not Takui how to change, in the eyes of the two lang God, he will always be the Hozen. Decorating mode dynamically attaches more responsibility to an object in a transparent manner to the customer. In other words, the client does not feel that the object is different before and after decorating. Adornment mode expands the functionality of an object without using the creation of more subclasses.
Java Dynamic Agent
Oh, and then mend. I can't see the design pattern anymore. into JavaScript.
Refer to Copying Source:
Http://www.cnblogs.com/java-my-life/archive/2012/04/23/2466712.html
http://blog.csdn.net/jackiehff/article/details/8621517
Http://www.cnblogs.com/machine/archive/2013/02/21/2921345.html
Java design Mode _ proxy mode (2016-08-19)