Java | 策略模式(反射动态) + 工厂模式

Maven添加依赖:

		<!-- 用于动态扫描 -->
        <dependency>
			<groupId>org.reflections</groupId>
			<artifactId>reflections</artifactId>
			<version>0.9.12</version>
		</dependency>

接口类:

package com.test.pageMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author HelSon
 * @class BasePageMethod
 * @description 业务处理接口
 * @date 2021-05-10
 **/
public interface BasePageMethod {
    void init(HttpServletRequest request, HttpServletResponse response);

    void run();
}

抽象类:

package com.test.pageMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author HelSon
 * @class BasePageMethod 
 * @description 业务处理基类
 * @date 2021-05-10
 **/
public interface BasePageMethod {
    void init(HttpServletRequest request, HttpServletResponse response);

    void run();
}

实现类:(需要创建一个文件夹implement,用于给工厂类扫描)

package com.test.pageMethod.implement;

import com.test.pageMethod.ABasePageMethod;

import java.util.Map;

/**
 * @author HelSon
 * @class TestPageMethod
 * @description 测试1
 * @date 2021-05-10
 **/
public class TestPageMethod extends ABasePageMethod {
    // 设置跳转页面名称
    @Override
    public void setViewName() {
        this.viewName = "test1";
    }

    // 执行的业务内容
    @Override
    public void run() {
        System.out.println("执行"+ this.getViewName() +"的run方法");
        System.out.println("页面名称:" + this.getViewName());
        System.out.println("请求参数:");
        // 请求
        Map Param = request.getParameterMap();
        Param.forEach((k, v)->{
            System.out.println("    Key:"+ k +", Value:"+ v);
        });
    }
}

工厂类:

package com.test.pageMethod;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;

/**
 * @author HelSon
 * @class PageMethodFactory
 * @description 业务处理工厂
 * @date 2021-05-10
 **/
public class PageMethodFactory {
    PageMethodFactory() {}
    static final Logger log = LoggerFactory.getLogger(PageMethodFactory.class);
    // 存储业务处理
    static Map<String, ABasePageMethod> pageMethodMaps = Maps.newHashMap();

    static {
        // 扫描该路径下的类
        Reflections reflections = new Reflections("com.test.pageMethod.implement");
        //获取继承了ABasePageMethod的所有类
        Set<Class<? extends ABasePageMethod>> classSet = reflections.getSubTypesOf(ABasePageMethod.class);
        for (Class<? extends ABasePageMethod> aClass : classSet) {
            putMethod(aClass);
        }   
    }

    // 执行实现方法
    public static void runMehthod(String methodType, HttpServletRequest request, HttpServletResponse response){
        if(methodType == null) {
            return;
        }
        // 拥有对应工厂及方法
        if (pageMethodMaps.containsKey(methodType)) {
            // 初始化并执行业务内容
            pageMethodMaps.get(methodType).init(request, response);
            pageMethodMaps.get(methodType).run();
        }
    }

    // 添加页面处理逻辑
    private static void putMethod(Class aBasePageMethod) {
        try{
            ABasePageMethod pageMethod = (ABasePageMethod) aBasePageMethod.newInstance();
            if(StringUtils.isBlank(pageMethod.getViewName())){
                log.error(pageMethod.getClass().getName() +"类没有设置name!");
            }
            pageMethodMaps.put(pageMethod.getViewName(), pageMethod);
        } catch (Exception e){
            log.error("业务处理添加异常!{}" ,e.getMessage(), e);
        }
    }
}

调用验证:

package com.test;

import com.test.pageMethod.PageMethodFactory;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 页面跳转
 */
@Controller
@RequestMapping("test")
public class TestPage{

    @RequestMapping("/{method}")
    public String go(@PathVariable String method, HttpServletRequest request, HttpServletResponse response){
        PageMethodFactory.runMehthod(method, request, response);
        return "/test/" + viewName;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值