title: C++ Singleton Design pattern alternatives
body: <p>I hate to beat a dead horse, that said, I've gone over so many conflicting articles over the past few days in regards to the use of the singleton pattern.</p>
  
  <p>This question isn't be about which is the better choice in general, rather what makes sense for my use case.</p>
  
  <p>The pet project I'm working on is a game. Some of the code that I'm currently working on, I'm leaning towards using a singleton pattern.</p>
  
  <p>The use cases are as follows:</p>
  
  <ul>
  <li>a globally accessible logger.</li>
  <li>an OpenGL rendering manager.</li>
  <li>file system access.</li>
  <li>network access.</li>
  <li>etc.</li>
  </ul>
  
  <p>Now for clarification, more than a couple of the above require shared state between accesses. For instance, the logger is wrapping a logging library and requires a pointer to the output log, the network requires an established open connection, etc.</p>
  
  <p>Now from what I can tell it's more suggested that singletons be avoided, so lets look at how we may do that. A lot of the articles simply say to create the instance at the top and pass it down as a parameter to anywhere that is needed. While I agree that this is technically doable, my question then becomes, how does one manage the potentially massive number of parameters? Well what comes to mind is wrapping the different instances in a sort of "context" object and passing that, then doing something like <code>context-&gt;log("Hello World")</code>. Now sure that isn't to bad, but what if you have a sort of framework like so:</p>
  
  <pre><code>game_loop(ctx)
     -&gt;update_entities(ctx)
          -&gt;on_preupdate(ctx)
               -&gt;run_something(ctx)
                   -&gt;only use ctx-&gt;log() in some freak edge case in this function.
          -&gt;on_update(ctx)
              -&gt;whatever(ctx)
                   -&gt;ctx-&gt;networksend(stuff)
     -&gt;update_physics(ctx)
          -&gt;ctx-&gt;networksend(stuff)
          //maybe ctx never uses log here.
  </code></pre>
  
  <p>You get the point... in some areas, some aspects of the "ctx" aren't ever used but you're still stuck passing it literally everywhere in case you may want to debug something down the line using logger, or maybe later in development, you actually want networking or whatever in that section of code.</p>
  
  <p>I feel like the above example would much rather be suited to a globally accessible singleton, but I must admit, I'm coming from a C#/Java/JS background which may color my view. I want to adopt the mindset/best practices of a C++ programmer, yet like I said, I can't seem to find a straight answer. I also noticed that the articles that suggest just passing the "singleton" as a parameter only give very simplistic use cases that anyone would agree a parameter would be the better way to go.</p>
  
  <p>In this game example, you probably wan't to access logging everywhere even if you don't plan on using it immediately. File system stuff may be all over but until you build out the project, it's really hard to say when/where it will be most useful.</p>
  
  <p>So do I:</p>
  
  <ol>
  <li>Stick with using singletons for these use cases regardless of how "evil/bad" people say it is.</li>
  <li>Wrap everything in a context object, and pass it literally everywhere. (seems kinda gross IMO, but if that's the "more accepted/better" way of doing it, so be it.)</li>
  <li>Something completely else. (Really lost as to what that might be.)</li>
  </ol>
  
  <p>If option 1, from a performance standpoint, should I switch to using namespace functions, and hiding the "private" variables / functions in anonymous namespaces like most people do in C? (I'm guessing there will be a small boost in performance, but then I'll be stuck having to call an "init" and "destroy" method on a few of these rather than being able to just allow the constructor/destructor to do that for me, still might be worth while?)</p>
  
  <p>Now I realize this may be a bit opinion based, but I'm hoping I can still get a relatively good answer when a more complicated/nested code base is in question.</p>
  
  <p><strong>Edit:</strong>
  After much more deliberation I've decided to use the "Service Locator" pattern instead. To prevent a global/singleton of the Service Locator I'm making anything that may use the services inherit from a abstract base class that requires the Service Locator be passed when constructed.</p>
  
  <p>I haven't implemented everything yet so I'm still unsure if I'll run into any problems with this approach, and would still love feedback on if this is a reasonable alternative to the singleton / global scope dilemma.</p>
  
  <p>I had read that Service Locator is also somewhat of an anti-pattern, that said, many of the example I found implemented it with statics and/or as a singleton, perhaps using it as I've described removes the aspects that cause it to be an anti-pattern?</p>