适配器模式

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();
    }
}

你可能感兴趣的:(适配器模式,java)