/**
* 静态代理
*/
public class Proxy {
public static void main(String[] args) {
Rent host = new Host();
WatchHostProxy watchHost = new WatchHostProxy(host);
ContractProxy contractProxy = new ContractProxy(watchHost);
contractProxy.rent();
}
}
//抽象接口,行为抽象化
interface Rent{
void rent();
}
//被代理类
class Host implements Rent{
@Override
public void rent() {
System.out.println("房东出租房");
}
}
//看房代理类
class WatchHostProxy implements Rent{
private Rent rent;
public WatchHostProxy(Rent rent) {
this.rent = rent;
}
public void rent(){
rent.rent();
watchHost();
}
public void watchHost(){
System.out.println("中介带你看房");
}
}
//签合同代理
class ContractProxy implements Rent{
private Rent rent;
public ContractProxy(Rent rent) {
this.rent = rent;
}
public void rent(){
rent.rent();
contract();
}
public void contract(){
System.out.println("代理签署合同");
}
}
Java 静态代理演示代码
最新推荐文章于 2022-09-18 14:27:54 发布