Static proxy to figure out three relationships: Client class proxy class delegate class
The behavior of the proxy class is similar and stronger than the delegate class (the implementation method has 1. Inheritance, 2. The proxy class implements a common interface with the delegate Class (preferred))
/**
* Complete log operation with static proxy mode
* @author Administrator
*
*/
public class Useraction {
Private Userdao Userdao = Userdaofac.newuserdao ();
public void Add () {
System.out.println ("========useraction========");
Userdao.add ();
}
public static void Main (string[] args) {
New Useraction (). Add ();
}
}
Public interface Userdao {//interface
public void Add ();
}
public class Userdaoimpl implements userdao{delegate class
@Override
public void Add () {
System.out.println ("=====userdao======");
SYSTEM.OUT.PRINTLN ("Completion log operation!!!"); /Intrusive Implantable code
}
}
public class Userdaofac {//Factory
public static Userdao Newuserdao () {
return new Userpowerproxy (new Userlogproxy (New Userdaoimpl ()));
}
}
public class Userlogproxy implements Userdao {//proxy class
Private Userdao Userdao;
Construction injection
Public Userlogproxy (Userdao Userdao) {
Super ();
This.userdao = Userdao;
}
@Override
public void Add () {
Userdao.add ();//The work of the entrusted class.
SYSTEM.OUT.PRINTLN ("added log operations");
}
}
public class Userpowerproxy implements Userdao {//proxy class
Private Userdao Userdao;
Construction injection
Public Userpowerproxy (Userdao Userdao) {
Super ();
This.userdao = Userdao;
}
@Override
public void Add () {
System.out.println ("Add a permission to judge");
Userdao.add ();
}
}
Static proxy of design mode proxy mode