java的代理(靜態代理和動態代理實例)

靜態代理

 

靜態代理:由程序員創建或特定工具自動生成源代碼,也就是在編譯時就已經將接口,被代理類,代理類等確定下來。在程序運行之前,代理類的.class文件就已經生成。

 

public interface Person {

       voidgiveMoney();

}

 

public class Student implements Person {

   private String name;

   public Student(String name) {

       this.name = name;

    }

       @Override

       publicvoid giveMoney() {

              System.out.println(name+ "上交班費50元");

 

       }

 

}

 

public class StudentsProxy implementsPerson {

 

       Studentstu;

      

       publicStudentsProxy(Person stu) {

              if(stu.getClass()== Student.class) {

                     this.stu= (Student)stu;

              }

       }

       @Override

       publicvoid giveMoney() {

              stu.giveMoney();

             

       }

 

}

 

public class StaticProxyTest {

 

       publicstatic void main(String[] args) {

              Personzhangsan = new Student("張三");

              Personmontor =  new StudentsProxy(zhangsan);

              montor.giveMoney();

 

       }

 

}

動態代理

動態代理模式主要由四個元素共同構成:

 

1. 接口,接口中的方法是要真正去實現的

2. 被代理類,實現上述接口,這是真正去執行接口中方法的類

3. 代理類,實現InvocationHandler,幫助被代理類去實現方法

4. 測試用例:

public interface ArithmeticCalculator {

   public int add(int i, int j);

    public int sub(int i, int j);

   public int mul(int i, int j);

   public int div(int i, int j);

}

 

public class ArithmeticCalculatorImplimplements ArithmeticCalculator {

 

       @Override

       publicint add(int i, int j) {

              intresult = i + j;

              returnresult;

       }

 

       @Override

       publicint sub(int i, int j) {

              intresult = i - j;

              returnresult;

       }

 

       @Override

       publicint mul(int i, int j) {

              intresult = i * j;

              returnresult;

       }

 

       @Override

       publicint div(int i, int j) {

              intresult = i / j;

              returnresult;

       }

 

}

 

public classArithmeticCalculatorLoggingProxy {

   private ArithmeticCalculator target;

   public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {

           this.target = target;

    }

public ArithmeticCalculatorgetLoggingProxy() {

           ArithmeticCalculator proxy = null;

           //代理對象由哪一個類加載器加載

           ClassLoader loader =target.getClass().getClassLoader();

           //代理對象類型,即其中有哪些方法

           Class [] interfaces = new Class[]{ArithmeticCalculator.class};

           //當調用代理對象其中方法,該執行代碼

           InvocationHandler h = newInvocationHandler() {

       /**

       *proxy:正在返回的那個代理對象,一般情況下在invoke方法中都不使用該對象

       *method:正在被調用的方法

       *args:調用方法時傳入的參數

       */

       @Override

       publicObject invoke(Object proxy, Method method, Object[] args) throws Throwable {

       Stringmethodname = method.getName();

       System.out.println("Themethod " + methodname + "brgins with" + Arrays.asList(args));

                            Objectresoult = method.invoke(target, args);

                            System.out.println("Themethod " + methodname + "end with" + resoult);

                            returnresoult;

                     }

              };

           proxy = (ArithmeticCalculator)Proxy.newProxyInstance(loader, interfaces, h);

           return proxy;

    }

}

 

public class Main {

 

       publicstatic void main(String[] args) {

              ArithmeticCalculatortarget = new ArithmeticCalculatorImpl();

              ArithmeticCalculatorproxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();

              intresult = proxy.add(1, 2);

              System.out.println(result);

       }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章