■メリット
元のクラスと同じインタフェースと同じメソッドを持たせることで、
元のクラスのメソッドに中間的な処理を持たせることができる。
■実装例
/*User.java*/
public class User {
public static void main(String[] args) {
Common_Interface com = new Proxy();
com.testmethod();
}
}
/*Common_Interface.java*/
public interface Common_Interface {
void testmethod();
}
/*ClassA.java*/
public class ClassA implements Common_Interface{
public void testmethod() {
System.out.println("ClassA");
}
}
/*Proxy.java*/
public class Proxy implements Common_Interface{
Common_Interface com = new ClassA();
public void testmethod() { //ClassAと同じインタフェース同じメソッドを実装する
com.testmethod(); //ClassAの内容を使う
System.out.println("proxy"); //プロキシ自体が自分独自の内容を自由に盛り込む
}
}
最終更新:2021年07月08日 20:26