ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

设计模式最优实践—策略模式

2021-12-12 23:02:04  阅读:160  来源: 互联网

标签:return 策略 ItemHandler 实践 handler import 最优 设计模式 public


什么策略模式

策略模式就是定义一组策略,分别在不同的类中封装,每种策略可以根据当前的场景互相替换,策略的之间的变化是独立的。比如我们要从杭州到上海,可以根据自身的情况,来选择是乘坐火车、汽车、高铁,这些出行方式就是不同的策略。

合适使用策略模式

当代码中的if else 超过3层情况,是时候考虑使用策略模式的代替了,好的代码是符合单一职责和开闭原则的。 

怎么使用策略模式

第一步,定义策略接口,包括3个方法:

1、获取策略方法

2、是否执行策略逻辑,这个后面方便管理

3、处理策略逻辑的方法

package com.example.caffe.hello.biz.designmode.strategy;

/**
 * 策略接口
 */
public interface ItemHandler<T,R> {

    /**
     * 商品的标识
     * @return
     */
    String getItemCode();


    /**
     * 是否执行
     * @param context
     * @return
     */
    boolean needExecute(T context);


    /**
     * 执行
     * @param context
     * @return
     */
    R execute(T context);


}

第二步,相关策略的实现

package com.example.caffe.hello.biz.designmode.strategy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class NormalItemHandler implements ItemHandler<ItemRequestContext,ItemResponseDTO>{
    @Override
    public String getItemCode() {
        return "normalItem";
    }

    @Override
    public boolean needExecute(ItemRequestContext context) {
        return true;
    }

    @Override
    public ItemResponseDTO execute(ItemRequestContext context) {

        log.info("NormalItemHandler execute....");

        ItemResponseDTO responseDTO = new ItemResponseDTO();
        responseDTO.setSuccess(true);
        return responseDTO;
    }
}
package com.example.caffe.hello.biz.designmode.strategy;

import lombok.Data;


/**
 * 策略的执行上下文
 */
@Data
public class ItemRequestContext {

    private String  itemCode;


}
package com.example.caffe.hello.biz.designmode.strategy;

import lombok.Data;

/**
 * 策略的响应结果
 */

@Data
public class ItemResponseDTO {


    private boolean success;
}

第三步,建立策略的简单工厂

package com.example.caffe.hello.biz.designmode.strategy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class ItemHandlerFactory {


    private static final Map<String,ItemHandler> ITEM_HANDLER_MAP = new HashMap<>(16);


    /**
     * 根据商品code,返回对应的handler
     * @param itemCode
     * @return
     */
    public ItemHandler getHandler(String itemCode) {

        return ITEM_HANDLER_MAP.get(itemCode);

    }


    @Autowired
    public void setItemHandlers(List<ItemHandler> handlers) {
        if (CollectionUtils.isEmpty(handlers)) {
            return;
        }
        for (ItemHandler handler : handlers) {
            log.info("init handler={},success",handler.getItemCode());

            System.out.println("init handler="+handler.getItemCode()+",success");
            ITEM_HANDLER_MAP.put(handler.getItemCode(),handler);
        }
    }

}

基于spring 容器,将所有的ItemHandler自动注册到ITEM_HANDLER_MAP,从而Spring容器启动完成后,getHandler 方法可以直接通过itemCode获取对应的ItemHandler。

第四步, 使用&测试

package com.example.caffe.hello.biz.designmode.strategy;


import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ItemProcessService {

    @Resource
    private ItemHandlerFactory itemHandlerFactory;



    public ItemResponseDTO processItem() {

        ItemRequestContext itemRequestContext = new ItemRequestContext();

        ItemHandler itemHandler = itemHandlerFactory.getHandler(itemRequestContext.getItemCode());


        if (itemHandler.needExecute(itemRequestContext)) {

            return (ItemResponseDTO) itemHandler.execute(itemRequestContext);

        }
        return null;
    }

}

Factory 职责是获取handler,hanler 只负责处理具体的逻辑,service 只负责逻辑编排,从而实现”低耦合高内聚“。至此策略模式介绍完了,

标签:return,策略,ItemHandler,实践,handler,import,最优,设计模式,public
来源: https://blog.csdn.net/wusd1256/article/details/82951568

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有