• AOP:静态代理实现方式①通过继承②通过接口


    文件结构:

    添加日志:

    package com.wangcf.manager;
    
    public class LogManager {
        
        public void add(){
            System.out.println("添加日志...");
        }
    }

    权限检查:

    package com.wangcf.manager;
    
    public class AuthorityManager {
        public boolean isAllown(){
            boolean flag=true;
            System.out.println("权限检查..");
            return true;
        }
    }

    ①:通过继承方式

    基类:

    package com.wangcf.manager.proxy;
    
    import com.wangcf.entity.User;
    import com.wangcf.manager.imp.IUserManager;
    
    public class UserManager implements IUserManager{
        
        public void add(User user){
            System.out.println("UserManager add...");
        }
        
        public void del(User user){
            System.out.println("UserManager del...");
        }
        
        public void update(User user){
            System.out.println("UserManager update...");
        }
    }

    Proxy1继承基类

    package com.wangcf.manager.proxy;
    
    import com.wangcf.entity.User;
    import com.wangcf.manager.LogManager;
    
    public class UserManagerProxy1 extends UserManager{
        
        private LogManager logManager;
        
        public UserManagerProxy1() {
            // TODO Auto-generated constructor stub
            this.logManager=new LogManager();
        }
        @Override
        public void add(User user) {
            // TODO Auto-generated method stub
            this.logManager.add();
            super.add(user);
        }
    }

    Proxy2继承Proxy1

    package com.wangcf.manager.proxy;
    
    import com.wangcf.entity.User;
    import com.wangcf.manager.AuthorityManager;
    
    public class UserManagerProxy2 extends UserManagerProxy1{
        
        private AuthorityManager autManager;
        
        public UserManagerProxy2() {
            // TODO Auto-generated constructor stub
            this.autManager=new AuthorityManager();
        }
        @Override
        public void add(User user) {
            // TODO Auto-generated method stub
            this.autManager.isAllown();
            super.add(user);
        }
    }

    Proxy3这是继承的另一种方式:

    package com.wangcf.manager.proxy;
    
    import com.wangcf.entity.User;
    import com.wangcf.manager.AuthorityManager;
    import com.wangcf.manager.LogManager;
    
    public class UserManagerProxy3 extends UserManager{
        
        private LogManager logManager;
        private AuthorityManager autManager;
        
        public UserManagerProxy3() {
            // TODO Auto-generated constructor stub
            this.autManager=new AuthorityManager();
            this.logManager=new LogManager();
        }
        @Override
        public void add(User user) {
            // TODO Auto-generated method stub
            this.autManager.isAllown();
            this.logManager.add();
            super.add(user);
        }
    }

    测试类:

    package com.wangcf.test;
    
    import com.wangcf.manager.proxy.UserManager;
    import com.wangcf.manager.proxy.UserManagerProxy1;
    import com.wangcf.manager.proxy.UserManagerProxy2;
    import com.wangcf.manager.proxy.UserManagerProxy3;
    /**
     * 继承方式静态代理
     * @author fan
     *
     */
    public class TestAction1 {
        public static void main(String[] args) {
            //只是实例不一样,这个是用代理继承方式实例化的
            UserManager userManager1=new UserManagerProxy1();
            userManager1.add(null);
            System.out.println("===========================");
            UserManager userManager2=new UserManagerProxy2();
            userManager2.add(null);
            System.out.println("===========================");
            UserManager userManager3=new UserManagerProxy3();
            userManager3.add(null);
        }
    }

    结果:

    ②通过接口

     接口:

    package com.wangcf.manager.imp;
    
    import com.wangcf.entity.User;
    
    public interface IUserManager {
        
        public void add(User user);
        
        public void del(User user);
        
        public void update(User user);
    }

     实现接口

    Proxy1

    package com.wangcf.manager.imp;
    
    import com.wangcf.entity.User;
    import com.wangcf.manager.LogManager;
    
    public class UserManagerProxy1 implements IUserManager{
    
        private IUserManager userManager;
        private LogManager logManager;
        
        public UserManagerProxy1(IUserManager userManager) {
            // TODO Auto-generated constructor stub
            this.userManager=userManager;
            this.logManager=new LogManager();
        }
        
        
        @Override
        public void add(User user) {
            // TODO Auto-generated method stub
            this.logManager.add();
            userManager.add(user);
        }
    
        @Override
        public void del(User user) {
            // TODO Auto-generated method stub
            this.logManager.add();
            userManager.del(user);
        }
    
        @Override
        public void update(User user) {
            // TODO Auto-generated method stub
            this.logManager.add();
            userManager.update(user);
        }
        
    }

    Proxy2

    package com.wangcf.manager.imp;
    
    import com.wangcf.entity.User;
    import com.wangcf.manager.AuthorityManager;
    
    public class UserManagerProxy2 implements IUserManager{
    
        private IUserManager userManager;
        private AuthorityManager authorityManager;
        public UserManagerProxy2(IUserManager userManager) {
            // TODO Auto-generated constructor stub
            this.userManager=userManager;
            this.authorityManager=new AuthorityManager();
        }
        
        @Override
        public void add(User user) {
            // TODO Auto-generated method stub
            this.authorityManager.isAllown();
            userManager.add(user);
        }
    
        @Override
        public void del(User user) {
            // TODO Auto-generated method stub
            this.authorityManager.isAllown();
            userManager.del(user);
        }
    
        @Override
        public void update(User user) {
            // TODO Auto-generated method stub
            this.authorityManager.isAllown();
            userManager.update(user);
        }
        
    }

    测试类:

    package com.wangcf.test;
    
    import com.wangcf.manager.imp.IUserManager;
    import com.wangcf.manager.imp.UserManagerProxy1;
    import com.wangcf.manager.imp.UserManagerProxy2;
    import com.wangcf.manager.proxy.UserManager;
    /**
     * 实现接口的方式静态代理
     * @author fan
     *
     */
    public class TestAction2 {
        public static void main(String[] args) {
            UserManager userManager = new UserManager();
            IUserManager iUserManager=new UserManagerProxy1(userManager);
            iUserManager.add(null);
            System.out.println("1=============");
            //和上面的一样
            IUserManager userManager2=new UserManagerProxy1(new UserManager());
            userManager2.add(null);
            System.out.println("2===========");
            IUserManager userManager3=new UserManagerProxy2(userManager2);
            userManager3.add(null);
        }
    }

    打印:

  • 相关阅读:
    GCD 使用说明
    iOS操作系统的层次结构
    Objective-C 类型转换
    UIView和Masonry实现动画效果
    iOS 常用手势
    SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)
    HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
    NBUT 1525 Cow Xor(01字典树+前缀思想)
    HDU 4825 Xor Sum(经典01字典树+贪心)
    HDU 1069 Monkey and Banana(二维偏序LIS的应用)
  • 原文地址:https://www.cnblogs.com/-beauTiFul/p/6289901.html
Copyright © 2020-2023  润新知