//静态代理设计实现
interface UserService {
    public void update();
}

class UserImpl implements UserService {
    @Override
    public void update() {
        System.out.println("修改用户..");
    }
}

class UserProxy implements UserService {
    UserService userService;

    public UserProxy(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void update() {
        //操作之前拓展逻辑
        userService.update();
        //操作之后拓展逻辑
    }
}                                    

运行:

public static void main(String[] args) {                  
    UserProxy proxy = new UserProxy(new UserImpl());      
    proxy.update();                                       
}                                                         

缺点:需要为每个代理类实现接口

 

Rick  32浏览  0评论  2020-09-26

上一篇