Java design mode--proxy mode
Proxy mode proxies: to provide a proxy for other objects and to control access to the object, like the relationship between the broker and the star, the broker is the star's proxy class. The simple thing is to do the processing before and after the method call, AOP idea, the advantage is not to change the original class method on the basis of dynamic add other methods.
3 Roles for proxy mode
1. Abstract role 2. Real role
3. Proxy roles
1. Static Proxy
The proxy class invokes the method of the proxy class.
2. Dynamic Agent---More commonly used
Public interface People {
void Eat ();
}
Public class Zhangsan implements people {
public void Eat () {
System.out.println ("eat");
}
}
Proxy class---Need to implement Invocationhandler interface
public class Porxyhandler implements Invocationhandler {
people people = NULL;
Public Porxyhandler (People people) {
This.people = people;
}
public object invoke (object proxy, Method method, object[] args)
Throws Throwable {
This.before ();
Method.invoke (people, args);
This.after ();
return null;
}
private void before () {
System.out.println ("hand washing");
}
private void After () {
SYSTEM.OUT.PRINTLN ("Wash the dishes");
}
}
public class Client {
public static void Main (string[] args) {
People people = (people) proxy.newproxyinstance (Classloader.getsystemclassloader (), New Class[]{people.class}, new Proxyhandler (New Zhangsan ()));
People.eat ();
}
}
Manuscripts: Diligent Learning qkxue.net
Extended reading:
Java design mode--proxy mode
Http://qkxue.net/info/40792/java
Java design mode--Adapter mode
Http://qkxue.net/info/40791/java
Java design pattern--Combined mode
Http://qkxue.net/info/40772/java
Java design pattern--responsibility chain model
Http://qkxue.net/info/40531/java
Java design mode--proxy mode