I'm tasked with changing some procedural Java code into Object Oriented re-usable module. It's an XML generator utility which uses velocity for XML generation. There are also business rule enrichments, all of which is configurable in Database tables. I have already removed huge if-else statements for rules and made them into Rule Objects with Abstract classes and interfaces which other Applications can extend and implement. However I'm facing difficulty with re-designing the overall class - XMLGeneratorUtil, which takes a JDBC connection , Logger and Properties in its constructor and does a hell lot of things in the constructor like building different Data structures(containing static data) from database.
Now, I know its not good to connect to Database from Constructor, but if I change to some thing like below:
XMlGeneratorUtil xmlUtil = new XMLGeneratorUtil();
xmlUtil.initialize(Conenriches conn, Properties prop , Logger logger) // Do DB Connections and all initializations here
xmlUtil.generateXML(data); // Data is a HashMap containing busines data that goes into the XML
How can I be sure that the users of my utility call initialize before they call generateXML? Should I create some Wrapper that internally creates a lightweight object and then initializes it without the user knowing it?
Also , is it good for my re-usable library to accept Connection as a parameter? Idea is that the calling Application of my utility may choose to manage their own Connection , so they just pass it to my API
Against the SRP (Single Responsibility principle) the XMLGeneratorUtil manipulates business data(which ultimately goes into XML data) as well as generates XML. Should the data enrichment be exposed as different API? (And then the user calls XMLGeneratorUtil)