编写一个接口,实现代理设计模式(Proxy)

interface NetWork{
	public void name();
}

class Demo1 implements NetWork{

	@Override
	public void name() {
		System.out.println("调用Demo1");
	}
	
}

class Demo2 implements NetWork{
	
	private NetWork netWork;
	
	public Demo2(NetWork netWork) {
		this.netWork = netWork;
	}
	
	public void check() {
		System.out.println("调用Demo2");
	}

	@Override
	public void name() {
		this.check();
		this.netWork.name();
	}
	
}

public class Demo {
	public static void main(String[] args) {
		NetWork netWork = null;
		netWork = new Demo2(new Demo1());
		netWork.name();
	}
}

输出结果为:

调用Demo2
调用Demo1

你可能感兴趣的:(#,设计模式)