Background: I’m using Google Guice and so it’s easier to pass through the configuration class but I think this is not the best way.
I have a configuration class which stores some paths:
class Configuration{
String getHomePath();
String getUserPath();
}
Also I have a class “a” which needs the “homepath” and a class “b” which needs the “userpath”.
Is it better to pass the configuration class through the constructor of class a and b or only pass through the specific path?
There’s no single answer to your question, there are only options to choose from, based on your specific situation.
If you know your
Configurationclass is going to grow AND if it’s likely for yourAandBclasses will use more from it, then pass the wholeConfigurationobject to their constructors. NB: I know this is against the YAGNI principle but sometimes you may know you’re gonna need it 😉Otherwise, you can consider using
@Namedinjection of your paths so that you reduceAandBclasses dependencies to their minimum, which is a good design practice.