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

什么策略模式

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

合适使用策略模式

当代码中的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 只负责逻辑编排,从而实现”低耦合高内聚“。至此策略模式介绍完了,

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值