I'm trying to determine the right approach to a design issue, but I am struggling to find something 'right'.
In simple terms:
- I have multiple functional classes that each need to interact with multiple common classes. (all created on the heap with New)
Eg
A class [a] object needs to be able to call methods in class [x], [y] & [z] objects
A class [b] object needs to be able to call methods in class [x], [y] & [z] objects
A class [c] object needs to be able to call methods in class [x], [y] & [z] objects
I have 3 solutions so far:
- Make the common classes (x, y, z) global objects (or a singletons) so the functional classes (a, b, c) can just call the objects directly. However, I dislike the idea of making things global.
- Pass pointers to the common classes (x, y, z) to each of the functional classes (a, b, c) during initialisation for later use. However, if a common class is deleted, the functional classes have stale pointers.
- Have a single global object (perhaps a singleton) that 'manages' the common classes (x, y, z), and the functional classes (a, b, c) request a common class pointer as/when needed from this global object. This allows them to know if the pointer is NULL immediately.
Previously, I've used (2), but over time I've become unhappy with this approach.
I think I'm currently favouring (3) but still uncomfortable with having a global object/singleton.
I've been searching around for a design pattern that fits this kind of scenario, but can't find anything that fits, maybe due to not knowing the right keywords.
Is there a common design approach/pattern to this problem?
Aucun commentaire:
Enregistrer un commentaire