Java设计模式:Proxy代理模式

版权声明:本文为Zhang Phil原创文章,请不要转载! https://blog.csdn.net/zhangphil/article/details/88719302

public interface Sourceable {
	void method();
}

public class Source implements Sourceable{
	@Override
	public void method() {
		System.out.println("Source method");
	}
}
public class Proxy implements Sourceable {
	private Source source;

	public Proxy() {
		this.source = new Source();
	}

	@Override
	public void method() {
		before();
		source.method();
		atfer();
	}
	
	private void before() {
		System.out.println("Proxy before");
	}

	private void atfer() {
		System.out.println("Proxy atfer");
	}
}
public class Test {
	public static void main(String[] args) {
		try {
			Test test = new Test();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Test() {
		Sourceable source = new Proxy();
		source.method();
	}
}

输出:

Proxy before
Source method
Proxy atfer

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/88719302
今日推荐