代理模式1

/**
 * 代理模式是用一個代理類對象代替原對象執行原來對象的方法
 * 在執行過程中可以根據需要執行相關的其他代碼
 * @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();
    }
}


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