设计模式之Proxy(代理模式)

Proxy(代理模式)

定义RealSubject和Proxy共用的接口
package com.dzeay.pattern.proxy;

public abstract class Subject {
    public abstract void excute();
}


定义Proxy所代表的实体
package com.dzeay.pattern.proxy;

public class RealSubject extends Subject {

    @Override
    public void excute() {
        System.out.println("RealSubject.excute");
    }

}


代理类
package com.dzeay.pattern.proxy;

public class Proxy extends Subject {

    private Subject subject;

    public Proxy(Subject subject) {
        this.subject = subject;
    }

    @Override
    public void excute() {
        subject.excute();
    }

}


Proxy -- 测试类
package com.dzeay.pattern.proxy;

/**
 * <pre>
 * Proxy(代理模式)详解:
 * 为其它对象提供一种代理以控制对这个对象的访问
 * </pre>
 * 
 * @author <a href="mailto:[email protected]">dzeay.com</a>
 * @since 2011-02-26
 * @version 1.0
 */
public class TestClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Subject subjectProxy = new Proxy(new RealSubject());
        subjectProxy.excute();
    }

}

你可能感兴趣的:(java,设计模式)