Now lets see our RootHistogramFactory Singleton
class RootHistogramFactory : public HistogramFactory {
static RootHistogramFactory *buildFactory();
static int deleteFactory();
RootHistogramFactory::RootHistogramFactory();
RootHistogramFactory::~RootHistogramFactory();
static RootHistogramFactory *single;
RootHistogramFactory* RootHistogramFactory::buildFactory()
if (single == 0) single = new RootHistogramFactory();
- Nobody can construct an instance of the object because the constructor is protected.
- The only way to get an instance is the static “buildFactory” member function (this function can call the protected constructor)
- it will create at most one instance and subsequent requests will receive a pointer to the only existing instance.
- We keep track of how may copies of the pointer were given out
- Only if the access count is 0 we allow deleting the object again (not shown).