• JAVA【设计模式】外观模式


    一、定义

    在这里插入图片描述

    外观模式:隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口,这种设计属于结构式设计,向外部提供了一个接口,来隐藏系统的复杂性。

    二、示例:

    模拟场景:
    1、模拟对服务接口添加白名单的场景,公司需要对服务接口的白名单设计,那么如果在每⼀个接⼝中都添加白名单人员信息这样的逻辑,就会⾮常麻烦且不易维护。另外这是⼀类具备通⽤逻辑的共性需求,⾮常适合开发成组件,以此来治理服务,让研发⼈员更多的关⼼业务功能开发。这时我们可以考虑用aop的思想去实现
    在这里插入图片描述

    基础部分

    @RestController
    public class HelloWorldController {
    
        @Value("${server.port}")
        private int port;
    
        /**
         * @DoDoor 自定义注解
         * key:需要从入参取值的属性字段,如果是对象则从对象中取值,如果是单个值则直接使用
         * returnJson:预设拦截时返回值,是返回对象的Json
         * 

    * http://localhost:8080/api/queryUserInfo?userId=1001 * http://localhost:8080/api/queryUserInfo?userId=小团团 */ @RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET) public UserInfo queryUserInfo(@RequestParam String userId) { return new UserInfo("虫虫:" + userId, 19, "天津市南开区旮旯胡同100号"); } }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    传统编码方式

    每个接口的内部都做白名单逻辑代码的编写,这样导致代码冗余,重复不可维护

    package com.qf.apprance.tradition;
    
    
    import com.qf.apprance.domain.UserInfo;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class HelloWorldController {
    
        public UserInfo queryUserInfo(@RequestParam String userId) {
    
            // 做白名单拦截
            List<String> userList = new ArrayList<String>();
            userList.add("1001");
            userList.add("aaaa");
            userList.add("ccc");
            if (!userList.contains(userId)) {
                return new UserInfo("1111", "非白名单可访问用户拦截!");
            }
    
            return new UserInfo("虫虫:" + userId, 19, "天津市南开区旮旯胡同100号");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    外观模式设计

    自定义注解@DoDoor,key为用户的白名单信息,returnJson为非白名单用户被拦截信息

    package com.qf.apprance.design.annotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface DoDoor {
         String key() default "" ;
    
        String returnJson() default "";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    自动配置到application.yml文件下面,后期修改只需要修改配置文件

    itstack:
      door:
        enabled: true
        userStr: 1001,aaaa,ccc #白名单用户ID,多个逗号隔开
    
    • 1
    • 2
    • 3
    • 4

    读取配置文件的内容,并把值输入到StarterService

    @ConfigurationProperties("itstack.door")
    public class StarterServiceProperties {
    
        private String userStr;
    
        public String getUserStr() {
            return userStr;
        }
    
        public void setUserStr(String userStr) {
            this.userStr = userStr;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    @Configuration
    @ConditionalOnClass(StarterService.class)
    @EnableConfigurationProperties(StarterServiceProperties.class)
    public class StarterAutoConfigure {
    
        @Autowired
        private StarterServiceProperties properties;
    
        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnProperty(prefix = "itstack.door", value = "enabled", havingValue = "true")
        StarterService starterService() {
            return new StarterService(properties.getUserStr());
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    public class StarterService {
    
        private String userStr;
    
        public StarterService(String userStr) {
            this.userStr = userStr;
        }
    
        public String[] split(String separatorChar) {
            String[] split = this.userStr.split(separatorChar);
            return split;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    AOP切面处理@DoDoor逻辑信息

    @Aspect
    @Component
    public class DoJoinPoint {
    
        private Logger logger= LoggerFactory.getLogger(DoJoinPoint.class);
    
        @Autowired
        private StarterService starterService;
    
        @Around(value = "@annotation(com.qf.apprance.design.annotation.DoDoor)")
        public Object doAop(ProceedingJoinPoint joinPoint) throws Throwable {
            Method method = getMethod(joinPoint);
            DoDoor annotation = method.getAnnotation(DoDoor.class);
            String filedValue = getFiledValue(annotation.key(), joinPoint.getArgs());
            logger.info("itstack door handler method:{} value:{}", method.getName(), filedValue);
            if (null == filedValue || "".equals(filedValue)) return joinPoint.proceed();
    
            String[] split = starterService.split(",");
            for (String s : split) {
                if (s.equals(filedValue)){
                    return joinPoint.proceed();
                }
            }
    
    
            return returnObject(annotation,method);
        }
    
        private Method getMethod(JoinPoint jp) throws NoSuchMethodException {
            Signature sig = jp.getSignature();
            MethodSignature methodSignature = (MethodSignature) sig;
            return getClass(jp).getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        }
    
        private Class<? extends Object> getClass(JoinPoint jp) throws NoSuchMethodException {
            return jp.getTarget().getClass();
        }
    
        //获取属性值
        private String getFiledValue(String filed, Object[] args) {
            String filedValue = null;
            for (Object arg : args) {
                try {
                    if (null == filedValue || "".equals(filedValue)) {
                        filedValue = BeanUtils.getProperty(arg, filed);
                    } else {
                        break;
                    }
                } catch (Exception e) {
                    if (args.length == 1) {
                        return args[0].toString();
                    }
                }
            }
            return filedValue;
        }
    
        //返回对象
        private Object returnObject(DoDoor doGate, Method method) throws IllegalAccessException, InstantiationException {
            Class<?> returnType = method.getReturnType();
            String returnJson = doGate.returnJson();
            if ("".equals(returnJson)) {
                return returnType.newInstance();
            }
            return JSON.parseObject(returnJson, returnType);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    package com.qf.apprance.web;
    
    import com.qf.apprance.design.annotation.DoDoor;
    import com.qf.apprance.domain.UserInfo;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * 公众号:bugstack虫洞栈 | 沉淀、分享、成长,让自己和他人都能有所收获!
     * 博  客:http://bugstack.cn
     * Create by 小傅哥 on @2020
     */
    @RestController
    public class HelloWorldController {
    
        @Value("${server.port}")
        private int port;
    
        /**
         * @DoDoor 自定义注解
         * key:需要从入参取值的属性字段,如果是对象则从对象中取值,如果是单个值则直接使用
         * returnJson:预设拦截时返回值,是返回对象的Json
         * 

    * http://localhost:8080/api/queryUserInfo?userId=1001 * http://localhost:8080/api/queryUserInfo?userId=小团团 */ @DoDoor(key = "userId", returnJson = "{\"code\":\"1111\",\"info\":\"非白名单可访问用户拦截!\"}") @RequestMapping(path = "/api/queryUserInfo", method = RequestMethod.GET) public UserInfo queryUserInfo(@RequestParam String userId) { return new UserInfo("虫虫:" + userId, 19, "天津市南开区旮旯胡同100号"); } }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    UML关系图

    在这里插入图片描述
    总结:
    以上我们通过中间件的⽅式实现外观模式,这样的设计可以很好的增强代码的隔离性,以及复⽤性,不仅使⽤上⾮常灵活也降低了每⼀个系统都开发这样的服务带来的⻛险。
    可能⽬前你看这只是⾮常简单的⽩名单控制,是否需要这样的处理。但往往⼀个⼩⼩的开始会影响着后续⽆限的扩展,实际的业务开发往往也要复杂的很多,不可能如此简单。因⽽使⽤设计模式来让代码结构更加⼲净整洁。

  • 相关阅读:
    【下载共享文件】Java基于SMB协议 + JCIFS依赖下载Windows共享文件(亲测可用)
    我的创作纪念日(一周年)
    02-2.3.2_1 单链表的插入和删除
    Linux以系统服务的方式启动Kafka(其他服务同理)
    Ansys Lumerical | 自发参量下变频 (SPDC) 光子源
    为何建议房企开发楼盘小程序?
    Spring的注解@Qualifier用法与简析
    SRM管理系统是什么?能为企业带来什么效益
    关于#网络协议#的问题:哪位知道京东跳转qq支付代付 会的联系我哪位知道京东跳转qq支付代付 会的联系我
    安卓系统中的进程介绍
  • 原文地址:https://blog.csdn.net/cativen/article/details/126201979