我正在创建一个复杂的java服务,它将analytics,goahead,news,awesome,aws,keep it up,m
和Connection
作为输入。此Document
用于实例化Connection
和ObjectDAO
个对象,然后由其他类用于获取相关数据。示例代码:
Configuration
我正在尝试减少各个类之间的耦合,但却无法提出任何好的逻辑。当你在运行时数据之前处理静态/可用时,诸如public class MyService{
public Document solve(Connection conn, Document input, Document input2){
OwnConfiguration config = new OwnConfiguration(conn);
ObjectDAO oDAO = new ObjectDAO(conn);
List<String> entityList = oDAO.getList();
FirstDataCruncher fCruncher = new FirstDataCruncher(oDAO, config, input);
Map<String, Map<String, String> > usefulData = fCruncher.getData();
Map<String, String> usefulData2 = fCruncher.getMoreData();
//Basically the crunchers get data from DB, store them in state variables which are used by other objects as and when needed.
SecondDataCruncher sCruncher = new SecondDataCruncher(oDAO, config, input2, usefulData2);
Map<String, String> usefulData3 = sCruncher.getData();
FinalSolver fSolver = new FinalSolver(usefulData1..2.3);
for(String i : entityList){
fSolver.finishThis(i, oDAO);
}
}
}
之类的DI框架是很好的,但对于这种情况,什么是一种好的方法呢?
我可以创建所有这些没有参数的类并将它们互相注入(通过setter传递Spring
,Connection
和其他args)但这似乎也是一个非常差的解决方案,因为我会有在运行任何逻辑之前,每个类都有多个setter行。
我该如何处理这种情况?