tags:

views:

199

answers:

4

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.

A: 

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 }
};
Shakedown
I am using this method right now, but I have to write <code>construct()</code> method in every descendant -- I consider it redundant, as it is obvious what it has to do.
samuil
+3  A: 

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.

anon
Using typical factory, like one you have provided, I'd have to add code to Make() method whenever I'll add new descendant class. That's what I consider redundant and harmful, as I want to provide my Base class as a closed framework.
samuil
No you don't. You can register new classes with the factory without changing factory code. This is a very common pattern - have you read a good book on this topic?
anon
@samuil: Consider using an abstract factory in which you register concrete factories at runtime. You code your abstract factory once (it allows registration of concrete factories) and you create a new concrete factory for each new type you want the abstract factory to create. That is quite a common pattern.
David Rodríguez - dribeas
@Neil Butterworth: I have read about patterns in Ruby. C++ is quite different and needs more patterns. Thanks for advices, solution starts to emerge ;)
samuil
+1  A: 

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...
1800 INFORMATION
Interesting approach. I am currently trying to see if it will fit my needs, and replace typical factory pattern mentioned by Neil Butterworth.
samuil
+1  A: 

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.
Igor Oks
Distinction between singleton and non-singleton descendants looks good, but you left my commented /* descendant constructor */. Because of this I would have to implement New method for each descendant anyway.
samuil