package cn.bunny.pattern17;

public class Proxy {
    public static void main(String[] args) {
        UserServiceProxy proxy = new UserServiceProxy(new UserServiceImpl());
        proxy.saveUser();
        proxy.findUser(1L);
    }

    interface UserService {

        /**
         * 保存用户
         */
        void saveUser();

        /**
         * 查找用户
         */
        Object findUser(Long id);
    }

    static class UserServiceImpl implements UserService {

        /**
         * 保存用户
         */
        @Override
        public void saveUser() {
            System.out.println("保存用户");
        }

        /**
         * 查找用户
         *
         * @param id id
         */
        @Override
        public Object findUser(Long id) {
            System.out.println("查找用户");
            return "User{name:'代理'}";
        }
    }

    static class UserServiceProxy implements UserService {

        private final UserService userService;

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

        /**
         * 保存用户
         */
        @Override
        public void saveUser() {
            userService.saveUser();
        }

        /**
         * 查找用户
         *
         * @param id id
         */
        @Override
        public Object findUser(Long id) {
            return userService.findUser(id);
        }
    }
}
