结合spring特性实现的策略模式

结合spring特性实现的策略模式

背景

工作中常常需要用到设计模式,集合spring实现的设计模式使用起来比较方便,下面就简单举例使用下。如我这里想根据不同的场景类型更新不同的表。

使用

  • 1、策略接口类
public interface UpdateTableStrategy {


    void UpdateTableByAnswer();
}
  • 2、不同的策略实现类:
@Component
public class updateTable1 implements UpdateTableStrategy{
@Override
    public void UpdateTableByAnswer() {
   System.out.println("策略1:更新表1");
    }
}

@Component
public class updateTable2 implements UpdateTableStrategy{
@Override
    public void UpdateTableByAnswer() {
   System.out.println("策略2:更新表2");
    }
}
  • 3、重点是这里,策略实例,或者说上下文
@Component
public class UpdateTableContext {
    private final Map<String,UpdateTableStrategy> strategyMap = new ConcurrentHashMap<>();

	//Spring4.0以后的版本支持泛型注入
    @Autowired
    public void strategyInterface(Map<String,UpdateTableStrategy> strategyMap){
        this.strategyMap.clear();
        for (Map.Entry<String,UpdateTableStrategy> entry : strategyMap.entrySet()) {
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
            this.strategyMap.put( entry.getKey(),entry.getValue());
        }
    }
    //Spring4.0以后的版本支持泛型注入
    @Autowired
    public void strategyInterface(List<UpdateTableStrategy> strategyMap){
        strategyMap.forEach(System.out::println);
    }
    public UpdateTableStrategy strategySelect(String mode){
        return this.strategyMap.get(mode);
    }
}
  • 4、使用
@Autowired
 UpdateTableContext updateTableContext;

//...

updateTableContext.strategySelect("updateTable1 ").UpdateTableByAnswer();

//更进一步优化:把参数用enum枚举类:
updateTableContext.strategySelect(Constant.MedInfo.getCode()).UpdateTableByAnswer();

再结合适配器和工厂更新不同的类型实体最终变为

//使用
QcAeoasTableAdapter qcTableAdapter = new QcAeoasTableAdapter(qcTableTmp);            qcTableFactory.getQcTableByTypeAndCode(reqMap.get("qcType").toString(),QcConstant.QcMedInfo.ADAS.getCode())
                    .UpdateTableByAnswer(qcTableAdapter,content, answerId,  authUserId, propertiesValue);

//工厂:
@Component
public class QcTableFactory {
    @Autowired
    UpdateTableContext updateTableContext;
    public UpdateTableStrategy getTableByType(String type){
        UpdateTableStrategy updateTableStrategy = null;
        if(Objects.equals(QcConstant.QcMedInfo.COPD.getCode(),code) && "1".equals(type)){
            updateTableStrategy = updateTableContext.strategySelect("qcTableReview");
        }
        if(Objects.equals(QcConstant.QcMedInfo.COPD.getCode(),code) && "0".equals(type)){
            updateTableStrategy = updateTableContext.strategySelect("qcTableHospital");
        }
        if(Objects.equals(QcConstant.QcMedInfo.ADAS.getCode(),code) && "0".equals(type)){
            updateTableStrategy = updateTableContext.strategySelect("qcAeoasTableHospital");
        }
        if(Objects.equals(QcConstant.QcMedInfo.ADAS.getCode(),code) && "1".equals(type)){
            updateTableStrategy = updateTableContext.strategySelect("qcAeoasTableReview");
        }
        return updateTableStrategy;
    }

//适配器 接口
public interface ITable {
    Long getTableId();
}

//适配器 抽象类
public abstract class AbstractTableAdapter<T> {
    public abstract T getTarget();
}
//适配器 继承类
public class QcTableAdapter extends  AbstractTableAdapter<QcTable> implements ITable{

    private QcTable qcTable;

    public QcTableAdapter(QcTable qcTable) {
        this.qcTable = qcTable;
    }
    @Override
    public QcTable getTarget() {
        return qcTable;
    }
    @Override
    public Long getTableId() {
        return qcTable.getId();
    }
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值