So I have my application consisting of a number of modules in a module hierarchy. Furthermore let’s also assume each module is a class and we have a tree of classes where the classes at the top are using the classes below, to make it more simple.
class A1 at the very bottom may depend on some input parameters.
class B1 is above
class A1 and is creating and using instances of
class A1. Therefore it has to pass the dependencies needed by the instances of
class A1 into them. If it can’t create these dependencies from some operations,
class B1 now has it’s on dependencies but additionally the dependencies of
The higher we go the more these dependencies will add up so that the toplevel
class will need to know all the dependencies unless they can be created at a lower level.
This means if
class A1 my program is dependent on the current
temperature, I have to pass this to the toplevel
class which then passes it to the next
class and so on until it arrives at the very bottom in
class A1. If I do that, I make the state explicit but it also means that I have methods or classes that take many parameters.
What if the
temperature is may change while the application is running – is there a way to avoid passing it all the way down the class tree without giving away explicit state? How would you guys handle this?
What if the
temperature is a constant that will never change while the program is running? Does this give us more options to avoid passing it always as an argument? I could see someone using a global configuration (singleton) but it will make it harder to test right?
I could also pass not the
temperature itself but a configuration object. This would mean
class B1 does not receive for example a
temperature and a
airpressure parameter but gets a configuration object passes it to
class B1 and
class B2 where
class B1 only needs the
class B2 only needs the
temperature. Is that a good approach? What are the pros/cons?