标签:out print inter string http 图片 sts 代理类 ace
静态代理是由程序员创建或特定工具自动生成源代码,在对其编译。在程序员运行之前,代理类.class文件就已经被创建了。动态代理是在程序运行时通过反射机制动态创建的。
静态代理模式由三个部分构成:
一个公共的接口、一个代理角色、一个被代理角色
接口:
public interface BuyHouse { void buyHouse(); }
接口实现类:
public class BuyHouseImpl implements BuyHouse { public void buyHouse() { System.out.println("我要买房"); } }
代理类:
public class BuyHouseProxy implements BuyHouse { private BuyHouse buyHouse; public BuyHouseProxy(final BuyHouse buyHouse) { this.buyHouse = buyHouse; } public void buyHouse() { System.out.println("代理类BuyHouseProxy buyHouse方法的调用前执行..."); buyHouse.buyHouse(); System.out.println("代理类BuyHouseProxy buyHouse方法的调用后执行..."); } }
测试类:
public class TestStaticProxy { public static void main(String[] args) { BuyHouse buyHouse = new BuyHouseImpl(); BuyHouseProxy buyHouseProxy = new BuyHouseProxy(buyHouse); buyHouseProxy.buyHouse(); } }
运行结果:
标签:out print inter string http 图片 sts 代理类 ace
原文地址:https://www.cnblogs.com/gaopengpy/p/11910526.html