由于现如今都是使用spingboot框架,这么现在就是基于springboot来实现工厂模式+策略模式
为了防止大量的if…else…或switch case代码的出现,可以使用策略模式+工厂模式进行优化。
在我的项目当中,报表繁多,所以尝试了这种方式进行优化报表的架构。代码很简单,如下:
工厂模式+策略模式-低配版
Factory工厂类
@Service
public class ReportFactory {
/**
* 初始化的时候将所有的ReportService自动加载到Map中
*/
@Autowired
private final Map reportIns = new ConcurrentHashMap<>();
public ReportService getReportIns(String code) {
ReportService reportInstance = reportIns.get(code);
if (reportInstance == null) {
throw new RuntimeException("未定义reportInstance");
}
return reportInstance;
}
}
接口
public interface ReportService {
S
本文地址:https://blog.csdn.net/lucky_love816/article/details/110479726
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!