# 代理模式
# 简介
结构型模式,通过对真实对象的多一层包装,可以完成很多操作,比如:统计性能、判断权限、记录日志等等。
# 示例
public interface Subject {
void doSomeThing();
}
public class RealSubject implements Subject{
@Override
public void doSomeThing() {
System.out.println("real subject do some thing");
}
}
public class SubjectProxy implements Subject {
private Subject subject;
public SubjectProxy(Subject subject) {
this.subject = subject;
}
@Override
public void doSomeThing() {
//添加扩展功能,比如统计耗时
long bg = System.currentTimeMillis();
subject.doSomeThing();
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - bg));
}
}
# 调用
@Test
public void tt(){
Subject subject = new SubjectProxy(new RealSubject());
subject.doSomeThing();
}