Search code examples
c++architecturesingletonfriendclass-design

How to implement a hierarchy class system


I'm a bit new to C++ way of handling this situation, so instead of using singleton pattern right away, I decided to ask this question instead to see if there's a better alternative.

Is there any other way to implement a system, where we got a main class that has access to a row of classes, that are friends in-between each other ? I didn't find any other way to make this work: inheritance won't work due to a fact that classes are working completely differently one from another, and using member classes is not an option iether, because then I will construct another instance of a same class, and that is not an option, due to a fact that I got a non-atomic class like logger, and there should be only one instance of it.

This is what I`m thinking of:

enter image description here


Solution

  • In very short:

    • YES it is possible to have a lot of classes that are friends in-between each other just as you showed on your diagram.

    • NO, it is not recommended to do so, since you'll have highly coupled system, and everytime you change something in one class, it might affect all the others.

    Design patterns such as the singleton pattern, and other techniques such as dependency injection are there to help to avoid such situations, and reduce coupling in order to facilitate maintenance and having a more robust system.