静态代理的实现-模拟中介代理房东出租房子给房客

package com.hpy.test;

/**
 * 房东
 */
public interface Landlord {
    public void house();
}
package com.hpy.test;

/**
 * 房东A
 */
public class LandlordA implements Landlord{
    public void house(){
        System.out.println("房东A出租美丽沙三室一厅房子");
    }
}
package com.hpy.test;

/**
 * 房东B
 */
public class LandlordB implements Landlord {
    public void house(){
        System.out.println("房东B出租西海岸三室一厅房子");
    }
}
package com.hpy.test;

/**
 * 中介
 */
public class Intermediary implements Landlord {
    private Landlord landlord;
    public Intermediary(Landlord landlord){
        this.landlord = landlord;
    }

    @Override
    public void house() {
        landlord.house();
    }
}
package com.hpy.test;

/**
 * 房客
 */
public class Client {
    public static void main(String[] args) {
      new Intermediary(new LandlordA()).house();
      new Intermediary(new LandlordB()).house();
    }
}

控制台打印:

          

猜你喜欢

转载自www.cnblogs.com/ithfm/p/9565512.html