1.静态代理
(1)角色分析
<1>抽象角色:一般使用角色和抽象类来实现
例:租房
<2>真实角色:被代理的角色
例:房东
<3>代理角色:代理真实角色
例:中介
<4>客户:使用代理模式来进行一些操作
例:客户
(2)代码实现
<1>租房
抽象角色一般用接口实现
public interface Rent {
public void rent();
}
<2>房东
//房东
public class Landlord implements Rent {
public void rent() {
System.out.println("房东要出租房子");
}
}
<3>中介
代理角色一般做一些附属的操作,如收中介费等。
//中介
public class Proxy {
//中介的参数为房东
private Landlord landlord;
public Proxy() {
}
public Proxy(Landlord landlord) {
this.landlord = landlord;
}
public void Rent() {
landlord.rent()