E
Eric
Perhaps this question has been posed before (I'd be surprised if it
hasn't) but I just gotta know...
Is it possible to combine the Singleton and Factory Method design
patterns in the same class?
Let's start with your basic Singleton class:
class Singleton
{
public:
static Singleton* Instance ();
};
Next, your basic Factory Method class:
class FactoryMethod
{
protected:
virtual FactoryMethod* Create () = 0;
};
The problem with combining the two is that you can't call a pure
virtual function from a static function:
class NotPossible
{
public:
static NotPossible* Instance ();
protected:
virtual NotPossible* Create () = 0;
};
static NotPossible* notPossible = 0;
NotPossible* NotPossible::Instance ()
{
if (notPossible == 0)
{
notPossible = Create (); // ERROR!
}
return (notPossible);
}
Is there a solution to this problem? If so, what is the solution?
Thanks,
Eric.
hasn't) but I just gotta know...
Is it possible to combine the Singleton and Factory Method design
patterns in the same class?
Let's start with your basic Singleton class:
class Singleton
{
public:
static Singleton* Instance ();
};
Next, your basic Factory Method class:
class FactoryMethod
{
protected:
virtual FactoryMethod* Create () = 0;
};
The problem with combining the two is that you can't call a pure
virtual function from a static function:
class NotPossible
{
public:
static NotPossible* Instance ();
protected:
virtual NotPossible* Create () = 0;
};
static NotPossible* notPossible = 0;
NotPossible* NotPossible::Instance ()
{
if (notPossible == 0)
{
notPossible = Create (); // ERROR!
}
return (notPossible);
}
Is there a solution to this problem? If so, what is the solution?
Thanks,
Eric.