1.工厂模式获取服务实例

背景:
不同的服务实例类型,具有相同的业务流程,可以根据入参来确定获取哪一个服务实例去执行业务流程

(工厂模式获取服务实例)

1.Controller 说明:根据String id这个入参获取对应处理job的实际serviceImpl实例

import com.up.test.service.InstanceService;
import com.up.test.service.InstanceServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FactoryController {

    @Autowired
    private InstanceServiceFactory instanceServiceFactory;

    @GetMapping("/getInstance/{id}")
    public String getInstance(@PathVariable("id") String id) throws Exception {
        InstanceService instance = instanceServiceFactory.getInstance(id);
        Object job = instance.job(id);
        return job.toString();
    }
}

2.服务实例工厂,项目启动的时候,服务对应的bean会被加载进instanceMap 中

import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 实例服务工厂类
 *
 * @param <T>
 */
@Service
public class InstanceServiceFactory<T> {

    /**
     * 初始化服务类
     */
    @Resource
    private final Map<String, InstanceService<T>> instanceMap = new ConcurrentHashMap<>();

    /**
     * 获取实现类
     */
    public InstanceService<T> getInstance(String id) throws Exception {
    
        InstanceService<T> instanceService = instanceMap.get(id);
        
        if (ObjectUtil.isNull(instanceService)) {
            throw new Exception("实例服务工厂获取异常,id编号未配置");
        }
        return instanceService;
    }
}

3.服务接口(对应三个实现)

/**
 * 服务接口
 *
 * @param <T>
 */
public interface InstanceService<T> {

    T job(String str) throws Exception;

    Object getConfig(String supplierNo);
}

4.抽象实例服务

public abstract class AbstractInstanceService<T> implements InstanceService<T> {
    /**
     * 获取配置文件,现在配置都在nacos配置,是否迁移到数据库中维护?
     */
    @Override
    public Object getConfig(String supplierNo) {
        return null;
    }

}

5.具体实现1:

import org.springframework.stereotype.Service;

@Service(value = "apple")
public class AppleInstanceService extends AbstractInstanceService {
    @Override
    public Object job(String id) {
        return "apple";
    }

}

6.具体实现2:

import org.springframework.stereotype.Service;

@Service(value = "banana")
public class BananaInstanceService extends AbstractInstanceService {

    @Override
    public Object job(String id) {
        return "banana";
    }
}

7.在controller 对应的/getInstance/{id}接口中,id替换为"apple" 或者 “banana”。在运行的时候,就会对应的输出其实现类中job 的输出值。
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值