策略、工厂模式融合 InitializingBean

@

策略、工厂模式融合 InitializingBean

策略、工厂模式分别是什么

策略模式

策略模式是将不同的算法封装成一个对象,这些不同的算法从一个抽象类或者一个接口中派生出来,客户端持有一个抽象的策略的引用,这样客户端就能动态的切换不同的策略

工厂模式

工厂模式又分为简单工厂和抽象工厂和工厂模式 ,这些工厂是为了创建对象而出现的,工厂模式创建不同的单个对象,而抽象工厂是为了创建不同的一些列的对象或者操作

策略+工厂解决的痛点是什么

结合工厂模式通过不同的类型,生产不同的对象和策略模式根据不同的对象,执行相同方法内的不同行为,来消灭大量的 if else 或 switch case ...,增强代码的可扩展性,便于代码质量维护

上代码

代码基础:springboot 2.3.2

这里以 Animal 为策略的接口,Cat、Dog为策略具体的实现

public interface Animal {
    void eat(String str);
}

/**
 * @Author charmsongo
 * @Create 2020/8/6 22:22
 * @Description 实现 InitializingBean ,便于 spring 容器时,初始化一次 afterPropertiesSet
 *              在 afterPropertiesSet 方法中把当前策略具体实现类注册到策略中维护的 map 中
 */
@Component
public class Cat implements Animal, InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(Cat.class);

    @Override
    public void eat(String str) {
        logger.info("cat eat yu, {}", str);
    }
    
    /**
     * 注册到策略模式维护的集合中
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        AnimalContext.registerService(AnimalEnum.CAT.getCode(),this);
    }
}

/**
 * @Author charmsongo
 * @Create 2020/8/6 22:24
 * @Description 实现 InitializingBean ,便于 spring 容器时,初始化一次 afterPropertiesSet
 *              在 afterPropertiesSet 方法中把当前策略具体实现类注册到策略中维护的 map 中
 */
@Component
public class Dog implements Animal, InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(Dog.class);

    @Override
    public void eat(String str) {
        logger.info("dog eat gutou, {}", str);
    }

    /**
     * 注册到策略模式维护的集合中
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        AnimalContext.registerService(AnimalEnum.DOG.getCode(),this);
    }
}

这里用枚举来作为 map 的 key,此处直接使用,不用过多关注

public enum AnimalEnum {

    CAT("01", "cat","猫"),
    DOG("02", "dog","狗");

    private String code;
    private String shortCode;
    private String msg;

    AnimalEnum(String code, String shortCode, String msg) {
        this.code = code;
        this.shortCode = shortCode;
        this.msg = msg;
    }

    public String getShortCode() {
        return shortCode;
    }

    public void setShortCode(String shortCode) {
        this.shortCode = shortCode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "AnimalEnum{" +
                "code='" + code + '\'' +
                ", shortCode='" + shortCode + '\'' +
                ", msg='" + msg + '\'' +
                '}';
    }

    public static boolean isValid(String code) {
        for (AnimalEnum value : AnimalEnum.values()) {
            if (value.getCode().equals(code)) {
                return true;
            }
        }
        return false;
    }
}

策略+工厂模式具体的操作

/**
 * @Author charmsongo
 * @Create 2020/8/6 22:28
 * @Description 策略+工厂
 */
public class AnimalContext {
    private static final Logger logger = LoggerFactory.getLogger(AnimalContext.class);
    private static Map<String, Animal> animalMap = new HashMap<String, Animal>();
    private Animal animal;

    public AnimalContext(String type) throws Exception {
        //此处判空,没有可以抛异常
        if (StringUtils.isEmpty(type) || !animalMap.containsKey(type)) {
            logger.error("type is error.");
            throw new Exception("type is error.");
        }
        animal = animalMap.get(type);
    }

    /**
     * 策略 eat 方法
     * @param str
     */
    public void eat(String str) {
        animal.eat(str);
    }

    /**
     * 策略注册方法
     * @param type
     * @param animal
     */
    public static void registerService(String type, Animal animal) {
        animalMap.put(type, animal);
    }
}

测试

咱们 web 应用一般都是以服务的方式使用的,这里也以服务的方式测试

新加一个 Controller 和 Request

@RestController
public class StrategyController {
    /**
     * http://localhost:8080/hello?code=01
     * @param animalRequest
     */
    @GetMapping("/hello")
    public void hello(AnimalRequest animalRequest) {
        try {
            AnimalContext animalContext  = new AnimalContext(animalRequest.getCode());
            animalContext.eat("哈哈");
            animalContext.run("喜喜");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


public class AnimalRequest implements Serializable {
    private static final long serialVersionUID = 3784925575397162170L;

    private String code;
    // 省去 set、get、toString 方法
}

启动 SpringBoot 工程的 Application 主类

浏览器访问 http://localhost:8080/hello?code=01

然后查看 IDE 控制台,出现 cat eat yu, 哈哈 即为成功!

2020-10-13 22:38:28.712  INFO 12584 --- [nio-8080-exec-2] cn.songo.strategydemo.service.impl.Cat   : cat eat yu, 哈哈

代码详情可参考源码,github:https://github.com/charmsongo/songo-code-samples/tree/master/strategydemo

posted @ 2020-10-13 17:12  charmsongo  阅读(246)  评论(0编辑  收藏  举报