I am creating a complex java service which gets a Connection
and a Document
as input. This Connection
is used to instantiate an ObjectDAO
and Configuration
objects which are then used by other classes to get relevant data. Sample code:
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);
}
}
}
I am trying to reduce the coupling between various classes but have been unable to come up with any good logic. DI frameworks such as Spring
are good when you can dealing with static/available before runtime data, but what is a good approach for such a situation?
I can create all these classes without arguments and inject them into each other (passing Connection
, inputxml
and other args via setters) but that seems like a pretty poor solution too, since I will have multiple setter lines per class before running any logic.
How should I tackle this scenario?