2

I have a class which currently defines a static variables that stores several parameters list.

static list<shared_ptr<ParameterContainer> > _containers

These ParameterContainers should be accessible by different clients.

The ParameterContainers are created by the constructor of the class, which receives a path as input, reads values from XML files contained in the path and stores the new ParameterContainers in _containers. The constructor control that the files have not been already stored.

Clients can access, modify and save on the XML files the parameters values. The variable _containers is protected by a mutex, which guarantee the correct shared access.

I think that having a central repository for this parameters is good, it avoids to loading it every time from a file. This is actually achieved by means of the static variable _containers.

I'm wondering if it would be better to implement the entire class a singleton and remove the keyword static from the variable _containters.

I read several discussions about the fact that the singleton could be an anti-pattern. I would like to know a good reason for avoiding singleton in this case.

PermanentGuest
  • 5,213
  • 2
  • 27
  • 36
Maverik
  • 2,358
  • 6
  • 34
  • 44
  • There could be a problem if multiple singletons are used and there are dependencies. E.g. depending on a logging singleton. It's difficult to ensure in which order they will be created and closed. This could lead to problems. – Patrik Beck Aug 19 '13 at 09:35
  • 1
    @AdamSangala: It's only a problem if the singleton relies on it's constructor to initialize its members. The typical implementation is to have a static pointer set to NULL (setting `static` variables to zero happens BEFORE constructors are called) to the instance of the singleton, and make the singleton into a functor. This means that the constructor for the singleton is called "manually" inside the functor, so it's safe. – Mats Petersson Aug 19 '13 at 09:47
  • I think you could use Scott Meyers implementation of Singleton in which "manual" instantiation is possible. http://stackoverflow.com/questions/1661529/is-meyers-implementation-of-singleton-pattern-thread-safe – user2672165 Aug 19 '13 at 10:10
  • That depends on your singleton religion. – PlasmaHH Aug 19 '13 at 10:17
  • @MatsPetersson The problem is with shutting down. Let's say you have 2 singletons A, and B, B uses A instance in it's destructor. (This is valid approach, let's say A is a logger) How do you ensure the order of destructors called for your singleton instances? – Patrik Beck Aug 19 '13 at 10:30
  • @AdamSangala: Then don't use the singleton's destructor to destroy the instance, just flush the file (if it's a logger) and leave the instance to be "leaked". Yes, it's ugly, but if you are using loggers in global objects destructors, then that's what you have to do. – Mats Petersson Aug 19 '13 at 10:34
  • @MatsPetersson: Sure, the described is a flawed design. I was just trying to illustrate that singletons can get very ugly if there are dependencies. – Patrik Beck Aug 19 '13 at 10:56
  • @PlasmaHH : I am 100% in agreement with your statement. I undestand what you mean. – user2672165 Aug 19 '13 at 11:02

0 Answers0