/**
* 代理模式是用一個代理類對象代替原對象執行原來對象的方法
* 在執行過程中可以根據需要執行相關的其他代碼
* @author
* @version 0.1
*/
public interface Sourceable {
void method();
}
/**
* @author
* @version 0.1
*/
public class User implements Sourceable {
@Override
public void method() {
System.out.println("The method() in User.");
}
}
/**
* @author
* @version 0.1
*/
public class Proxy implements Sourceable {
private User user;
public Proxy(){
super();
this.user = new User();
}
private void beforeMethod(){
System.out.println("This method is excuted before method.");
}
private void afterMethod(){
System.out.println("This method is excuted after method.");
}
@Override
public void method() {
this.beforeMethod();
user.method();
this.afterMethod();
}
}
/**
* @author
* @version 0.1
*/
public class Test {
public static void main(String[] args) {
Sourceable user = new Proxy();
user.method();
}
}