设计模式总览
代理模式
https://www.bilibili.com/video/BV1D7411i7mT?p=238
public class TestProxy {
public static void main(String[] args) {
UserDao userDao = new UserDao();
UserDaoProxy userDaoProxy = new UserDaoProxy(userDao);
userDaoProxy.add();
}
public interface Dao {
void add();
}
//被代理类
public static class UserDao implements Dao {
@Override
public void add() {
System.out.println("添加用户");
}
}
//代理类
public static class UserDaoProxy implements Dao {
private UserDao userDao;
public UserDaoProxy(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void add() {
System.out.println("add()开始执行");
long s = System.currentTimeMillis();
//核心业务逻辑要交给被代理者完成
userDao.add();
long e = System.currentTimeMillis();
System.out.println("add()执行结束");
System.out.println("add()耗时(ms):" + (e-s));
}
}
}