Proxy mode is the mode structure of the object. Proxy mode to a certain object provides a proxy object, the proxy object controlled by a reference to the original object.
Structure proxy mode
The so-called proxy is a person or institution on behalf of another person or organization to take action. In some cases, a customer does not want or can not directly refer to an object, and the object agent can act as an intermediary between the client and the target audience.
Proxy mode class diagram as follows:
In proxy mode roles:
● abstract object roles: It declares a common interface to the target object and proxy object, this way can be used proxy object can be used anywhere in the target object.
● target object roles: the definition of the target object proxy object represents.
● proxy object roles: agent inside the object containing the target object reference, so that the target object can be operated at any time; proxy object provides the same interface to a target object, the target object may be replaced so that at any time. Proxy objects are usually passed to the client calls before or after the target object, perform an action, rather than simply passes the call to the target object.
Source
Abstract Object Role
public abstract class AbstractObject { //操作 public abstract void operation(); }
Target Object Role
RealObject the extends AbstractObject {class public @Override public void Operation () { // some operating System.out.println ( "some operations"); } }
Proxy Object Role
ProxyObject the extends AbstractObject {class public RealObject realObject new new RealObject = (); @Override public void Operation () { before calling the target object related operation can be done // System.out.println ( "before"); realObject.operation (); / / audiences can be done after the call related operation System.out.println ( "after"); } }
Client
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub AbstractObject obj = new ProxyObject(); obj.operation(); } }
Proxy object can be seen from the above example will call the client's assigned to your audience, you can perform a particular operation before the method is called after with the target object.