Proxy pattern: When an object does not want or cannot directly reference another object, the proxy object can act as an intermediary between the client and the target object. Static proxy is specific to implement the need for proxy object interface, overwrite the method when personalized operation.
A static proxy is the implementation class for an interface that is implemented separately for the agent.
Not much to say, on the code?
Package com.ant;
Public interface Hello {
public void say ();
}
Package com.ant;
public class Helloimpl implements Hello {
@Override
public void Say () {
System.out.println ("Hellw world!");
}
}
Package com.ant;
public class Staticproxy implements Hello {
private Hello Hello;
@Override
public void Say () {
SYSTEM.OUT.PRINTLN ("Staticproxy welcome");
Hello.say ();
}
Public staticproxy (Hello Hello) {
This.hello = Hello;
}
}
Package com.ant;
public class Staticproxytest {
public static void Main (string[] args) {
Hello hello = new Helloimpl ();
Staticproxy staticproxy = new Staticproxy (hello);
Staticproxy.say ();
}
}
Summarize:
Static agent has a disadvantage, that is, when the method in the interface changes (new, the deletion of the necessary implementation method), all the proxy class must make the corresponding changes (assuming there are multiple proxy class case), undoubtedly increase the code complexity, reusability is low, this time can have dynamic agent.
Talking about static agent of-java design mode