CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Join Date
    Feb 2017
    Posts
    677

    Re: Two dimensional dynamic string array

    Quote Originally Posted by TubularX View Post
    Well, the advice from your link says:

    "If a dependency is ambient, meaning that it is used by many classes and/or multiple layers, use Singleton."

    I just don't think it's that simple.
    It sometimes is that simple indeed. Let's say you're writing an application that prints information to the standard output (std::cout) and say the printing is ambient meaning it can take place anywhere.

    Now, std::cout is a Singleton and as a convinced Dependency Injection aficionado you cannot just use it wherever you need it. Instead you inject it into every corner of your application so std::cout will end up in the parameter list of every constructor and every function.

    Faced with this design choice I would go for direct use of the Singleton. Injecting it would be a complication without benefits. It will not reduce dependencies nor will it make the code more reusable or readable. It will just add overhead.

    Admittedly this is a simplistic example. Mostly the situation is not that clear-cut. But one thing is certain. Dependency Injection is not the answer to everything. It should be handled with care as should Singleton and every other design pattern/methodology you can think of. There is no silver bullet in design.
    Last edited by wolle; July 31st, 2018 at 12:47 AM.

  2. #17
    Join Date
    Aug 2006
    Posts
    231

    Re: Two dimensional dynamic string array

    That sounds like "one way communication" as I mentioned in my previous post. The side effect of printing is probably only important to the user and doesn't affect the rest of the code (assuming that exceptions are unlikely), which makes it quite manageable. Still, writing unit tests to verify that the output is correct will be less straightforward.

    Indeed, there is no silver bullet, and I think in general you need to be careful with shared mutable state (where global is the worst kind).

    Another approach (if you are into functional programming, which seems to become more and more popular in C++) is to use monads to deal with side effects in a thread safe manner.
    Last edited by TubularX; July 31st, 2018 at 10:14 AM.

  3. #18
    Join Date
    Feb 2017
    Posts
    677

    Re: Two dimensional dynamic string array

    Quote Originally Posted by TubularX View Post
    That sounds like "one way communication" as I mentioned in my previous post.
    In my view the rule of thumb we are discussing holds for "multiway communication" too.

    Lets say for example you have a message framework. This is a convenient way to organize a large application. Any piece of code (module, object) can register a message box with the framework and start listening/acting on messages and sending its own messages.

    Such a message framework will be ambient and according to the rule of thumb a Singleton implementation should be considered, called Postoffice or something. Using Dependency Injection would be as inconvenient in this case as it was in my previous example because the Postoffice would have to be passed around to every corner of the application adding overhead with no benefits at all. I would definitely go for a Singleton Postoffice in a case like this.
    Last edited by wolle; August 2nd, 2018 at 11:01 PM.

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Two dimensional dynamic string array

    I think we're moved too far from the original thread topic and going down a rabbit hole. So I'm closing this thread. If there's a good reason to re-open, pm a mod.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured