代理模式之静态代理

package edu.aeon.proxy;

/**
 * 说明:静态代理 角色:真实角色、代理角色 条件:两种觉得必须实现同一接口 例子:1.北京找房(中介) 2.结婚(找婚庆公司)...等
 * 
 * @author lzj
 */
public interface Rent {
    void lease();
}

class Host implements Rent {
    /**
     * 主人:出租房屋
     */
    @Override
    public void lease() {
        System.out.println("房屋出租!");
    }

}

class Agent implements Rent {
    private Rent host;

    /**
     * 代理商(中介必须获得房屋出租权)
     * 
     * @param host
     */
    public Agent(Rent host) {
        this.host = host;
    }
    /**
     * 代理商出租房屋
     */
    @Override
    public void lease() {
        before();
        host.lease();
        after();

    }

    void before() {
        System.out.println("出租房屋前:带客户看房!");
    }

    void after() {
        System.out.println("出租房屋后:签订合同!");
    }

}

测试类:

package edu.aeon.proxy;

/**
 * 说明:静态代理模式
 * 
 * @author lzj
 *
 */
public class TestProxy {

    public static void main(String[] args) {
        Host host = new Host();
        // 代理商或者中介必须获得房主的授权才能出租房屋
        Agent agent = new Agent(host);
        agent.lease();
    }

}

 测试结果:

如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

原文地址:https://www.cnblogs.com/aeon/p/9742215.html