Here are my points:
There is a difference between having only one instance of the object in the entire application vs. an object becoming singleton so that any one calling
new()
will magically findtheInstance
.In your case, there is no benefit of having server being singleton because no one is creating server other than
main()
. It's just plain simple to have it as ordinary object. Even if it is singleton, it will be called only once!Parser
should never be singleton either for a different reason here. If i understood correctly,parser
's only role is whencore
is launched; Ideally it should followuse-and-throw
oatternpattern rather thansingleton
Even
parser
continues to serve some specific queries till corecore
wants it, by designparser
should be one instance percore
because same instance ofparser
should never share information about various differentcore
s. It makesparser
dedicated per corecore
.Most people design
logger
as a singleton. It is not really much big deal, however, if you are a multi-threaded system a singleton loggerlogger
is a big villienvillain. First off, every singleton needs to be threadsafe. Suppose even if you make it thread safe, every time athread x
calls logger it is busy doingprintf
forthread y
so as a resultthread x
waits where aswhereas ideally you should be worrying about getting the maximum out of your quad-Core system and serve as many requestrequests as possible.Ideally the only genuine case where
logger
should be centralized or singleton is where the exact order of events must be registered one-by-one. For most purposes, different files and messages with itstheir own time stampstimestamps are usually better even for debug as welldebugging.
So for me Server
(which is one instance) is created by main()
and core
, parser
and logger
are a tuple bornsthat is born and dies on per request (always together). Each tuple doesn't know the state or existence of other such tupletuples, and that is good so that arbitrary number of threads can be instantiated in parallel, (one thread per tuple) without loosinglosing scalability but preserving loose coupling.
Only thing is - it means you are not using any design pattern! But that's not a bad thing. It is not necessary that including a design pattern makes your design necessarily better.
Remember:
There are really rare reasons where using singleton becomes must; everywhere else, using singleton is always an invitation to problems.