Design Pattern Directory:
Design pattern-The observer pattern
Design Pattern 2--Decorator mode
Design pattern 3--iterators and Composite modes (iterators)
Design pattern 4--iterators and combined patterns (combination)
Design mode 5--Factory mode
Design mode 6--Single-piece mode
Design mode 7--Command mode
Design mode 8--adapter and appearance mode
Design pattern 9--Template Method mode
Design mode 10--State mode
Design mode 11--proxy mode
Overview
Basic concept of an agent model
Two
Reference
Basic concept of an agent model
The proxy mode provides an alias or placeholder for another object to control access to the object.
The proxy mode is used to create a delegate that allows the delegate object to control access to certain objects, which can be objects that are remote, create expensive objects, or objects that require security control.
Several agent-controlled access methods:
- Remote Agent Control access to remote objects (see the previous blog Java RMI instance)
- Virtual Agent Control access to create expensive resources
- Protection Agents Control access to resources based on permissions
Remote Agent
- Subject, for Realsubject and proxy interface, by implementing the same interface, proxy in the place where Realsubject appears to replace it
- Realsubject is the real object, it is the proxy and control access to the object.
- Proxy holds the Realsubject reference. In some cases, proxy will also be responsible for the creation and destruction of Realsubject objects. Both the client and the Realsubject must pass the proxy. Because proxy and realsbuject implement the same interface (Subject), So any place that uses realsubject can be replaced by proxy. Proxy also controls access to Realsubject, and in some cases we may need such control. These conditions include Realsubject is a remote object, Realsubject creation overhead, or realsubject need to be protected.
Source
Abstract Object Role
Package Cn.telling.rmi; /** * @author */publicabstractclass abstractobject {// Operation Public abstractvoid operation ();}
Target object Role
PackageCn.telling.rmi;/*** * @ClassName: realobject TODO *@authorXingle * @date 2015-9-29 a.m. 10:52:49*/ Public classRealobjectextendsAbstractobject {/*** * @Description: TODO *@authorXingle * @data 2015-9-29 a.m. 10:53:05*/@Override Public voidoperation () {//Some operationsSYSTEM.OUT.PRINTLN ("Some operations"); }}
Proxy Object Role
PackageCn.telling.rmi;/*** * @ClassName: Proxyobject * TODO *@authorXingle * @date 2015-9-29 a.m. 10:54:31*/ Public classProxyobjectextendsabstractobject{realobject Realobject=NewRealobject (); /*** * @Description: TODO *@authorXingle * @data 2015-9-29 a.m. 10:54:43*/@Override Public voidoperation () {SYSTEM.OUT.PRINTLN ("Invoke target object before operation"); Realobject.operation (); System.out.println ("Invoke target object after operation"); }}
Client
Package Cn.telling.rmi; /** * @author */Publicclass Client {publicstatic void Main (string[] args) { new proxyobject (); Obj.operation (); }}
Execution Result:
Virtual agent of two-agent mode
Reference:
"Design pattern" Learning Note 15: Proxy pattern
"Design pattern" Learning Note 16: Virtual proxy for Proxy mode (implementing CD cover Loader)
The agent model of Java and pattern
Design mode 11--proxy mode