0

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?

sudshekhar
  • 1,576
  • 3
  • 18
  • 31
  • Some [builder](https://en.wikipedia.org/wiki/Builder_pattern) maybe –  Apr 28 '16 at 11:47
  • But that wouldn't help me reduce coupling between the lower classes would it? Eg: `FinalSolver` creates a couple more classes `Helper1`, `Helper2`. Should I pass them via `MyService` itself? What about classes created by `Helper1` itself? Sorry if I am over-complicating things a bit, but I am thinking I need some kind of DI too (no idea how to do that though) – sudshekhar Apr 28 '16 at 11:52
  • Not everything needs to be under springs control, just because you can. I would however strive for not passing `Connection` objects around (assuming that that is a `javax.sql.Connection`, that generally isn't a good idea. However without knowing that information or seeing the implementation of for instance `OwnConfiguration` and the other objects it is hard to determine. – M. Deinum Apr 28 '16 at 11:57
  • So actually the `Connection` is taken from a `connection pool`. I am passing around this pool and getting the connection wherever needed. This is a legacy application and hence my hands are tied regarding the input. How would you recommend I should handle the interior dependencies then? I am finding it hard to test the application because of these interior classes and thus, was trying to figure out ways to decouple things – sudshekhar Apr 28 '16 at 12:02

0 Answers0