User loginNavigation |
Singleton classes really that bad?There was some recent discussion on here that mostly claimed that singleton classes, assuming they had state, were bad (I'm not quite sure what a stateless singleton would be other than a different naming convention for global functions). I'm unconvinced =) Stateful singletons still offer one advantage over straight global variables -- they have encapsulation. Granted, it's not any better than having a global object, but it does at least seem a little safer in that you can control access to it, cutting down a lot of the arguments against global variables in general. I'm not sure what nightmare scenarios people are thinking of when they say singletons can change unpredictably. Can someone clarify? I'm thinking of a class having C++ code something like this. For the sake of argument we'll say this is a design situation where we know we'll only need one of this class. class DataList { public: static void AddToList(ctl_node& node); static void Display(); static ListIter Begin(); static ListIter End(); protected: DataList(); ~DataList(); static DataList& Instance(); ListType nodeList; }; DataList& DataList::Instance() { static DataList theList; return theList; } void DataList::AddToList(ctl_node& node) { DataList& theList = Instance(); theList.nodeList.push_back(&node); } That's enough to see how the functions would get a reference to the one instance. Thoughts? By dataangel at 2006-01-07 23:31 | LtU Forum | previous forum topic | next forum topic | other blogs | 20245 reads
|
Browse archives
Active forum topics |
Recent comments
22 weeks 6 days ago
22 weeks 6 days ago
22 weeks 6 days ago
45 weeks 14 hours ago
49 weeks 2 days ago
50 weeks 6 days ago
50 weeks 6 days ago
1 year 1 week ago
1 year 6 weeks ago
1 year 6 weeks ago