Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As ever for this sort of subject, there is no answer that applies to every problems. Some said that singleton should be avoided when they are used as an access to a service. It is a use that is akin to the use of global variables. This way you mask the fact that you use service X in your implementation :</p> <pre><code>// in header class MyUsefulClass { public: void doSomethingUseful(); }; // in .cpp MyUsefulClass::doSomethingUseful() { // ... MyWonderfulService::instance().doService1(); // ... MyWonderfulService::instance().doService2(); } </code></pre> <p>You create a coupling with MyWonderfulService that the users of your class can not guess. Moreover, You can not easily test your useful class with a mock service ...</p> <p>That's why I usually prefer <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle" rel="nofollow">dependancy inversion</a> :</p> <pre><code>// in header class MyUsefulClass { public: void setServiceToUse(MyService&amp;); void doSomethingUseful(); // [...] }; // in .cpp MyUsefulClass::doSomethingUseful() { // ... _myService-&gt;doService1(); // ... _myService-&gt;doService2(); } </code></pre> <p>This way is usually considered better as the coupling between class is lighter. Nevertheless, for some services, which are well known to be of widespread use in a framework, it is simplier to use a singleton. It makes sense for a single service that is the service which give you access to all other services in a framework for example ^^ It is often use for technical services like logging for instance.</p> <p>my2c</p> <p>Edit: I read the article, as the focus is on AbstractFactories, the use of a singleton is a casual one, not a design decision. It is understandable in an article in which you do not want to write things that will not get you to your point.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload