Java设计模式(静态代理)

Java设计模式的核心 - 对多态的灵活应用。

//创建通用接口
interface ClothesFactory{
	void produceClothes();
}
//创建实现通用接口的代理类
class ProxyClothesFactory implements ClothesFactory{
	private ClothesFactory clothesFactory;
	
	public ProxyClothesFactory(ClothesFactory clothesFactory) {
		this.clothesFactory = clothesFactory;
	}

	@Override
	public void produceClothes() {
		this.clothFactory.produceClothes();
	}
	
}
//创建实现通用接口的实际执行任务类
class NikeFactory implements ClothFactory{

	@Override
	public void produceClothes() {
		
		//Do something before
		
		System.out.println("Producing Nike clothes.");
		
		//Do something after
		
	}
	
}
//测试类
public class StaticProxyTest {
	public static void main(String [] args) {
		ClothesFactory nikeFactory = new NikeFactory();
		ProxyClothesFactory proxyClothesFactory = new ProxyClothesFactory(nikeFactory);
		proxyClothesFactory.produceClothes();
	}
}

为什么称为静态代理呢?
被代理的类已经由代码决定,我们可以由反射实现动态代理。

发布了70 篇原创文章 · 获赞 4 · 访问量 3048

猜你喜欢

转载自blog.csdn.net/qq_34515959/article/details/104936136