1、可以使真实角色操作更纯粹不用去关心一些公共业务
2、公共业务交给代理角色! 实现了业务分工
3、业务发生拓展的时候,方便集中管理
被代理的类
package com.chirs.Demo;
public class Host implements RentInterface {
public void Rent() {
System.out.println("房东要租房子");
}
}
代理类
package com.chirs.Demo;
public class Proxy implements RentInterface {
private Host host;
public Proxy() {
}
public void setHost(Host host) {
this.host = host;
}
public Proxy(Host host) {
this.host = host;
}
public void Rent() {
SeeHourse();
host.Rent();
HeTong();
ShouFei();
}
public void SeeHourse() {
System.out.println("带看房");
}
public void HeTong() {
System.out.println("签合同");
}
public void ShouFei() {
System.out.println("收钱");
}
}
测试类
import com.chirs.Demo.Host;
import com.chirs.Demo.Proxy;
import com.chirs.Demo.RentInterface;
public class myTest1 {
public static void main(String[] args) {
Host host = new Host();
Proxy proxy = new Proxy();
proxy.setHost(host);
proxy.Rent();
}
}