If ten classes inherit from a base class, and all ten of the subclasses need access to a singleton, how should the reference to the singleton be passed. I see several ways of going about this, two such being:
-
Call a static method in the superclass that sets the reference of a static object which can then be shared by the subclasses
-
Pass a reference of the singleton to each subclass as an argument in their constructors. Each subclass could store a reference to the singleton object, or pass it into the superclass constructor.
But I don’t know what is the preferred way, or if I am missing something obvious. Thanks in advance.
Option 1 provides for less noise and duplication in your code.