【发布时间】:2021-08-17 11:31:08
【问题描述】:
我有几个服务具有由调度程序调用的相同方法。这是一项服务的一个示例。
@Service
public class MyService1 {
@Autowired
private MyLocalMapper1 localMapper1;
@Autowired
private MyLocalRepository1 localRepository1;
@Autowired
private MyExternalMapper1 externalMapper1;
@Autowired
private MyExternalRepository1 externalRepository1;
public void startProcess() {
//I use the mappers and the repositories in here
}
}
我有 15 个服务与这个服务完全相似,但每个服务都有一个特定的映射器(例如 MyLocalMapper2、MyLocalMapper3 等)以及存储库。
例如
@Service
public class MyService2 {
@Autowired
private MyLocalMapper2 localMapper2;
@Autowired
private MyLocalRepository2 localRepository2;
@Autowired
private MyExternalMapper2 externalMapper2;
@Autowired
private MyExternalRepository2 externalRepository2;
public void startProcess() {
//I use the mappers and the repositories in here
}
}
考虑到它使用的对象在每个服务中都不同,是否有任何设计模式允许重用 startProcess 方法中的代码?
我想为每个对象创建接口,例如:LocalMapperInterface、LocalRepositoryInterface 等。并将所有这些接口作为参数传递给单个方法,但不确定这是否是最佳方法。
提前致谢。
【问题讨论】:
-
需要了解更多有关 startProcess 方法的信息 - 它是否在所有服务中执行相同类型的操作?
标签: java spring generics design-patterns