-1

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

buttons.png
  • 77
  • 1
  • 9
  • There is nothing wrong with singletons IF they are used right! – quant Nov 12 '18 at 07:19
  • So, in this specific case it is allowed to make logger class by using singletone pattern, so everyone could around could use it, but what about in-between class interaction? Should I just use friend - friend way, or there is something else I could do? My primary goal is to make sure that the code base consists of "modules" that are doing what they are told to do, and if necessary, use methods of other classes to help with a task. – buttons.png Nov 12 '18 at 07:27
  • A logger is usually a good case for a singleton. A CRC32 class seems wrong, perhaps use functions in a namespace instead? Or if it's only a single function, then just keep it a single function. "Networking" seems to broad to be a single class, but could be a namespace with multiple classes (`Socket`, `ClientSocket` , `ServerSocket` etc., or something similar). And avoid `friend` as much as you can. And if `Archivation` is supposed to be a serialization class, consider e.g. [Boost serialization](https://www.boost.org/doc/libs/1_68_0/libs/serialization/doc/index.html) instead of making your own. – Some programmer dude Nov 12 '18 at 07:30
  • CRC32 is a bit more than a single function, its a set of functions build on top of CRC32 (like, for example, get crc from a file, get crc from a string, e.t.c.) Networking, indeed, is more than a single class as well, I'm trying to keep things simple so people could understand about what I`m talking. The only problem that's left is that I still need somehow to make sure that there will be only one instances of those modules presented in MainClass and they could still communicate. I'm trying to reuse as much code as I can, and keep ammount of instances as low as I can as well. – buttons.png Nov 12 '18 at 07:36

1 Answers1

3

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.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Ok, so on a level of CRC32, Networking and Arсhivation classes if I want to reduse the code base by using already implemented methods of other classes, as well as reduce ammount of instances and move away from two way coupling then I should stick with singleton pattern for them . Got it, thanks. – buttons.png Nov 12 '18 at 08:05