Tutorial :How to declare factory-like method in base class?



Question:

I'm looking for solution of C++ class design problem. What I'm trying to achieve is having static method method in base class, which would return instances of objects of descendant types. The point is, some of them should be singletons. I'm writing it in VCL so there is possibility of using __properties, but I'd prefer pure C++ solutions.

class Base {    private:      static Base *Instance;    public:      static Base *New(void);      virtual bool isSingleton(void) = 0;  }  Base::Instance = NULL;    class First : public Base { // singleton descendant    public:      bool isSingleton(void) { return true; }  }    class Second : public Base { // normal descendant    public:      bool isSingleton(void) { return false; }  }    Base *Base::New(void) {    if (isSingleton())      if (Instance != NULL)        return Instance = new /* descendant constructor */;      else        return Instance;    else      return new /* descendant constructor */;  }  

Arising problems:

  • how to declare static variable Instance, so it would be static in descendant classes
  • how to call descendant constructors in base class

I reckon it might be impossible to overcome these problems the way I planned it. If so, I'd like some advice on how to solve it in any other way.


Edit: some minor changes in code. I have missed few pointer marks in it.


Solution:1

Just to check we have our terminologies in synch - in my book, a factory class is a class instances of which can create instances of some other class or classes. The choice of which type of instance to create is based on the inputs the factory receives, or at least on something it can inspect. Heres's a very simple factory:

class A { ~virtual A() {} };     class B : public A {};  class C : public A {};    class AFactory {     public:        A * Make( char c ) {           if ( c == 'B' ) {               return new B;           }           else if ( c == 'C' ) {               return new C;           }           else {             throw "bad type";           }        }  };  

If I were you I would start again, bearing this example and the following in mind:

  • factorioes do not have to be singletons
  • factories do not have to be static members
  • factories do not have to be members of the base class for the hierarchies they create
  • factory methods normally return a dynamically created object
  • factory methods normally return a pointer
  • factory methods need a way of deciding which class to create an instance of

I don't see why your factory needs reflection, which C++ does not in any case support in a meaningful way.


Solution:2

Basing this on the answer by @Shakedown, I'll make Base be templated on the actual type, using the CRTP:

template <class T>  class Base  {  public:   static std::auto_ptr<Base<T> > construct()   {      return new T();   }  };    class First : public Base<First>  {  };    class Second : public Base<Second>  {  };  

This is nice because construct is now once again a static member. You would call it like:

std::auto_ptr<First> first(First::construct());  std::auto_ptr<Second> second(Second::construct());    // do something with first and second...  


Solution:3

You can create a Singleton class and a NonSingleton class, and make all the descendants inherit one of them.

class Base {    public:      static Base *New() = 0;  }    class SingletonDescendant: public Base {     public:      *Base::New() {        if (Instance != NULL)          return Instance = new /* descendant constructor */;        else          return Instance;      }    private:      static Base *Instance;  }  SingletonDescendant::Instance = NULL;    class NonSingletonDescendant: public Base {     public:      *Base::New() {        return new;      }  }    class First : public SingletonDescendant{ // singleton descendant  }    class Second : public NonSingletonDescendant{ // normal descendant  }  

It solves the issues that you raised:

  • How to declare static variable Instance, so it would be static in descendant classes: It exists only in the SingletonDescendant class.
  • How to call descendant constructors in base class: Using the New function
  • I have to write construct() method in every descendant; I consider it redundant, as it is obvious what it has to do: Now it is only in SingletonDescendant and NonSingletonDescendant.


Solution:4

How about something like this:

class Base  {  public:   virtual Base construct() = 0;  };    class First : public Base  {  public:   Base construct(){ return First(); // or whatever constructor }  };    class Second : public Base  {  public:   Base construct(){ return Second(...); // constructor }  };  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »