Quote Encyclopedia
That is, proxy pattern,23 one of the common object-oriented software design patterns. Proxy mode definition: Provides a proxy for other objects to control access to this object. In some cases, an object may not fit or directly refer to another object, and the proxy object can act as a mediator between the client and the target object. composition: abstract Role: A business method that declares a true role implementation through an interface or abstract class. Agent role: To implement abstract role, is the agent of real role, through the real role of the business logic method to implement abstract methods, and can attach their own operations. Real role: Implementing abstract roles, defining the business logic to be implemented by the real role for proxy role invocation. The proxy object provides an interface that is the same 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.
Specific implementation:
Code implementation:
1. Abstract role
Public abstract class Abstractobject {
//operation public
abstract void Method ();
}
2. Target object role
public class Realobject extends Abstractobject {
@Override public
Void Method () {
System.out.println () Specific action ");
}
3, Agent object role
public class Proxyobject extends Abstractobject {
private realobject real = new Realobject ();
@Override public
Void Method () {
//TODO auto-generated method stub
before ();
Real.method ();
After ();
}
Custom handling
private void before () {
System.out.println ("before ...") before
and after the method is executed;
private void After () {
System.out.println ("After ...");
}
4. Client-side test
public class Client {public
static void Main (string[] args) {
Proxyobject object = new Proxyobject ();
Object.Method ();
}
Through the simple proxy mode above, after running
Before .....
Specific operations
After .....
Through the proxy mode above, that is, the proxy object implementation delegates the client's call to the target object, and can perform a specific operation before and after the method of invoking the target object.