To solve this problem: in a number of modules to insert a function, for example, in different business processing modules, you need to check whether the user is logged in, assuming that the agent is not used, each add a module, you need to add a lot of code.
For example, in addition to inferring whether or not to log in, assuming that you need to add a logging function, then each module will need to add a function again.
Here we take advantage of static proxy capabilities to address this requirement.
Code structure
function function Interface
Package Itbuluoge.proxy;public interface Icompent {public void Bussiness1 ();p ublic void Bussiness2 ();p ublic void BUSSINESS3 ();}
proxy class
Package Itbuluoge.proxy;public class Proxycompent implements Icompent{private icompent ic;public proxycompent (ICompent IC) {this.ic=ic;} public void ValidateUser () {System.out.println ("Authenticate user ..."); public void Bussiness1 () {//TODO auto-generated method Stubvalidateuser (); Ic.bussiness1 ();} public void Bussiness2 () {//TODO auto-generated method Stubvalidateuser (); Ic.bussiness2 ();} public void Bussiness3 () {//TODO auto-generated method Stubvalidateuser (); Ic.bussiness3 ();}}
By proxy class
Package Itbuluoge.proxy;public class Compent implements Icompent{public void Bussiness1 () {//TODO auto-generated method s TUBSYSTEM.OUT.PRINTLN ("Business 1");} public void Bussiness2 () {//TODO auto-generated method StubSystem.out.println ("Business 2"); public void Bussiness3 () {//TODO auto-generated method StubSystem.out.println ("Business 3");}
Test class
Package Itbuluoge.proxy;public class Test {/** * @param args */public static void main (string[] args) {//TODO Auto-genera Ted Method Stubproxycompent myproxy=new proxycompent (New Compent ()); Myproxy.bussiness1 (); Myproxy.bussiness2 (); MYPROXY.BUSSINESS3 ();}}
Output results
We can see that if we need to add a common function in very many, we just need to add it to the proxy class. And it can be seen that the proxy class only has its own business logic, no matter what service, service is defined in the proxy class itself.
Static proxy for Java