设计模式-利用注入单例+策略+工厂模式实现对组件的选择

定义策略基类 IStrategy

首先创建基础策略接口,用于定义选择的规则accept(String type);

package com.diit.config.domain.module.factory;

public interface IStrategy {
    boolean accept(String type);
}

创建更为具体的策略接口 WidgetStrategy

package com.diit.config.domain.module.factory;

import com.alibaba.fastjson.JSONObject;
import com.diit.config.domain.module.entity.TApps;

public interface WidgetStrategy extends IStrategy {
    //定义所有组件共有的方法 存储组件
    void saveWidgetAppsLink (TApps apps, JSONObject dataBeanJson, Integer resourceType);
}

创建具体的实现类 1 2 3 等

BussinessServiceImpl
package com.diit.config.domain.module.service.impl;

/**
 * 保存功能组件与应用管理的关系
 */
@Service
//实现WidgetStrategy类,通过实现accept函数来判断工厂需求是否是自己
public class BussinessServiceImpl implements WidgetStrategy {
    @Resource
    private TAppcofigService appcofigService;
    //实现的具体的业务方法
    @Override
    public void saveWidgetAppsLink(TApps apps, JSONObject dataBeanJson, Integer resourceType) {
        //保存功能组件与应用管理的关系
        TAppcofig appcofig = new TAppcofig();
        appcofigService.save(appcofig);
    }

    @Override
    public boolean accept(String type) {
        //通过枚举获得自己的key值,返回需求的type是否是自己
        return StringUtils.equals(WidgetEnmu.Bussiness.getCode(), type);
    }
}

WidgetServiceImpl
package com.diit.config.domain.module.service.impl;

/**
 * 保存资源组件与应用管理的关系
 */
//实现WidgetStrategy类,通过实现accept函数来判断工厂需求是否是自己
@Service
public class WidgetServiceImpl implements WidgetStrategy {
    @Resource
    private TConfigValueService configValueService;
    //实现的具体的业务方法
    @Override
    public void saveWidgetAppsLink(TApps apps, JSONObject dataBeanJson, Integer resourceType){
        //保存资源组件与应用管理的关系
        configValueService.saveBatch(configValueList);
    }

    @Override
    public boolean accept(String type) {
        //通过枚举获得自己的key值,返回需求的type是否是自己
        return StringUtils.equals(WidgetEnmu.Widget.getCode(), type);
    }
}

定义枚举 enum

给各类具体策略设定key

package com.diit.config.domain.module.enmu;

public enum WidgetEnmu {
	Bussiness("1","功能组件"),
	Widget("2","资源组件");

	private String code;

	private String name;

	WidgetEnmu(String code, String name){
		this.code = code;
		this.name = name;
	}
}

创建工厂类 WidgetFactory

package com.diit.config.domain.module.factory;

import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
@Component
public class WidgetFactory {
    //通过@Resource来对List集合注入接受所有TestInterface接口的实现类,默认的注入为单例模式
    //如果是Map集合会接受所有TestInterface接口的实现类,key则为bean name,默认为第一个字母小写的类名
    @Resource
    private List<WidgetStrategy> widgetStrategyList;

    public WidgetStrategy get(String type) {
        //通过传递来的type值来对List中所有的实现类来进行判断,最后通过多态返回拥有具体实现类特性的接口类
        return widgetStrategyList.stream().filter(t -> t.accept(type)).findAny().get();
    }
}

具体使用

package com.diit.config.domain.module.service.impl;

@Service
public class TAppsServiceImpl extends ServiceImpl<TAppsMapper, TApps> implements TAppsService {
    @Resource
    private WidgetFactory widgetFactory;
    
    @Override
    public boolean addApps(Map<String, Object> map) {
        //通过使用工厂策略模式,使此处解耦,只需要传递类型就能够获取对应的具体实现类进行保存
        //如果新增其他类型,只需要在枚举中新增并增加具体的实现类就可以,不需要改动工厂代码也不需要改动此处的代码
       //resourceType 1表示地图工具(功能组件);2表示业务分析组件(资源组件);3表示资源目录树
        widgetFactory.get(String.valueOf(resourceType)).saveWidgetAppsLink(apps, dataBeanJson, resourceType);//idea
        return success;
    }
}

posted @   Ideaway  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示