
Jason Hise wrote:
Pavel and I have been discussing a sibling class to the singleton, namely a multiton. Such a class would manage a unique object instance for each key provided to GetInst. It should be able to use many of the same policies that the singleton uses.
Pavel thinks that it could be useful to allow GetInst to take any number of parameters. Would this actually be useful, and if so, how could such a design be achieved?
I think that "multitons" are very useful - but I'm not sure that a Singleton class should necessarily be bloated out to support them. The way I have normally implemented this type of structure is to have a class that is itself a singleton, but is responsible for generating and returning one of a fixed (or variable) number of classes. If you implement the interface through a static function that delegates through the singleton GetInst function, you get a very simple interface which doesn't detract from a nice simple singleton interface - eg: class Multiton : public Singleton<Multiton> { public: class Child; static Child *GetInst( int number ) { return GetInst()->GetChild( number ); } private: Child *GetChild( int number ) { // TODO: check for and return the correct instance of Child } }; Is this pretty much what you were thinking? Dave