Discussion-static proxy in Java design mode; discussion-java Design Mode
Proxy pattern: when an object does not want or cannot directly reference another object, the Proxy object can play a mediation role between the client and the target object. Static proxy is the proxy object interface required for specific implementation. It performs personalized operations when overwriting methods.
Static Proxy is used to implement a class of an interface for Proxy ).
Let's just move on to 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;
@ 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 ();
}
}
Summary:
One drawback of static proxy is that when methods in the interface are changed (when necessary implementation methods are added or deleted ), all proxy classes must be changed accordingly (assuming there are multiple proxy classes). This will undoubtedly increase Code complexity and reduce reusability. In this case, dynamic proxies can be created.