I have three possible designs and I'm trying to determine which one is better and in which cases they would be better. The general idea of all the designs is that there is a data object that stores some data and there is an analyzer object that analyzes the data.
I need to choose between these designs and I like Design 2 the best. However, a developer I'm working with is pushing for Design 3, which I think is the worst. Are there any advantages or disadvantages I'm missing for any of the designs? Let me know if clarification is needed.
Design 1
In design 1, DataAnalyzer has a Data object that is supplied during construction. When the client calls dataAnalyzer.analyze()
, the data is analyzed. In this design, each objects responsibility is clear; The Data object simply holds data and the DataAnalyzer object analyzes data. Changing the data stored will only change the Data class and adding types of analysis methods will only change the DataAnalyzer class. One problem with this design is that the DataAnalyzer object can only be used for the Data object passed in during construction, so if there are lots of data objects, lots of DataAnalyzers need to be created. Another disadvantage (which will be clearer from Design 3) is that the client needs to know about two classes instead of just one. If there are more classes that have an association with Data, the client will have to work will all of these classes.
Design 2
Design 2 is very similar to design 1, except now DataAnalyzer is reusable (for different data objects). The client still has to work with two classes as opposed to one as in design 3. The responsibilities are still very clear and maintenance is easy.
Design3
Design 3 allows the client to work with one object. The client can say data.analyze()
and not know anything about the DataAnalyzer. I'm not sure if this violates the single responsibility rule; the Data object has an interface that allows analysis, but the responsibility is really delegated to DataAnalyzer. Another issue is that a DataAnalyzer is created for every Data object created, regardless of whether the Data needs to be analyzed or not. Now if more functionality was added, a lot of things would change. If a DataPrinter class was created (let's assume this is better than having the data print the data itself), the client wouldn't have to worry about creating a dataPrinter object and calling dataPrinter.printData()
, it could just call data.print()
. However, by adding this class I had to change the interface of Data. Adding more methods to any of the DataXX classes causes methods to be added to the Data class.