• 适配器模式


    package com.cdkj.project.design.adapter;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * @author YUFEI
     * @ClassName HttpHandler
     * @description: 业务实现类
     * @date 2023年10月12日
     * @version: 1.0
     */
    public class HttpHandler {
        private static final Logger logger = LoggerFactory.getLogger(HttpHandler.class);
        public void doHandle(){
            logger.info("处理http请求");
        }
    }
    

    package com.cdkj.project.design.adapter;
    
    /**
     * @author YUFEI
     * @ClassName HttpAdapter
     * @description: 适配器
     * @date 2023年10月12日
     * @version: 1.0
     */
    public class HttpAdapter implements HandlerAdapter {
        @Override
        public boolean supports(Object handler) {
            return handler instanceof HttpHandler;
        }
    
        @Override
        public void handle(Object handler) {
            HttpHandler httpController  = (HttpHandler) handler;
            httpController.doHandle();
        }
    }
    
    package com.cdkj.project.design.adapter;
    
    /**
     * @author YUFEI
     * @ClassName HandlerAdapter
     * @description: 适配器模式
     * @date 2023年10月12日
     * @version: 1.0
     */
    public interface HandlerAdapter {
        boolean supports(Object handler);
        void handle(Object handler);
    }
    
    package com.cdkj.project.design.adapter;
    
    /**
     * @author YUFEI
     * @ClassName AnnotationAdapter
     * @description: 适配器
     * @date 2023年10月12日
     * @version: 1.0
     */
    public class AnnotationAdapter implements HandlerAdapter {
        @Override
        public boolean supports(Object handler) {
            return handler instanceof AnnotationHandler;
        }
    
        @Override
        public void handle(Object handler) {
            AnnotationHandler annotationController = (AnnotationHandler) handler;
            annotationController.doAnnotationHandle();
        }
    }
    
    package com.cdkj.project.design.adapter;
    
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * @author YUFEI
     * @ClassName AnnotationHandler
     * @description: 业务实现类
     * @date 2023年10月12日
     * @version: 1.0
     */
    public class AnnotationHandler {
        private static final Logger logger = LoggerFactory.getLogger(AnnotationHandler.class);
        public void doAnnotationHandle(){
            logger.info("处理Annotation请求");
        }
    
    }
    
    package com.cdkj.project.design.adapter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author YUFEI
     * @ClassName CustomDispatchServlet
     * @description: TODO
     * @date 2023年10月12日
     * @version: 1.0
     */
    public class CustomDispatchServlet {
        private final List handlerList = new ArrayList<>(10);
    
        public CustomDispatchServlet() {
            handlerList.add(new AnnotationAdapter());
            handlerList.add(new HttpAdapter());
        }
        public void doDispatch(){
            HttpHandler httpController = new HttpHandler();
            //选择适配器
            HandlerAdapter handler = getHandler(httpController);
            //调用适配器的处理方法
            handler.handle(httpController);
        }
        public HandlerAdapter getHandler(Object handler) {
            for (HandlerAdapter currentHanler : handlerList) {
                if (currentHanler.supports(handler)) {
                   // currentHanler.handle(handler);
                    return currentHanler;
                }
            }
            return null;
        }
        public static void main(String[] args) {
            CustomDispatchServlet customDispatchServlet = new CustomDispatchServlet();
            customDispatchServlet.doDispatch();
        }
    }
    
  • 相关阅读:
    3款堪称神器的电脑软件,简单又实用
    基于python高校选课系统设计与实现flask-django-nodejs-php
    FFmpeg入门详解之31:Ubuntu编译FFmpeg
    Pygame 精准检测图像碰撞
    node+express+multer实现单个或多个图片文件,视频文件上传
    spring 请求 出现实体类大小写不一致 出现的问题
    以一次 Data Catalog 架构升级为例聊业务系统的性能优化
    Qt QtCreator调试Qt源码配置
    1668、最大重复子字符串(暴力+KMP+strstr)
    如何修改glog由进程生成日志为按天输出日志
  • 原文地址:https://blog.csdn.net/qq_39716220/article/details/133824932