【Spring】静态代理
例子: 租房子
角色: 我 (I ) 中介( Proxy ) 房东( host )
Rent 接口
package org.example;public interface Rent {void rent();
}
房东
package org.example;public class Host implements Rent{@Overridepublic void rent() {System.out.println("房东要出租房子");}
}
中介
package org.example;public class Proxy implements Rent{private Host host;public Proxy(){}public Proxy(Host host) {this.host = host;}@Overridepublic void rent() {host.rent();}
}
I
package org.example;public class I {public static void main(String[] args) {Host host =new Host();Proxy proxy=new Proxy(host);proxy.rent();}}
核心: Proxy proxy=new Proxy(host);
将房东出租房子这件事交给了中介去做,但是出租房子还是房东
中介不仅只做一件事,比如要签合同什么的,方法写在Proxy类中