Singleton with Factory Method possible?

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.
 
V

Victor Bazarov

Eric said:
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?

So, in order to "Create" you need to have an instance (since it's non-
static). But in order to have an instance you want to call "Create"
first, right? Is there a solution to this problem?

V
 
P

Phlip

Eric said:
Let's start with your basic Singleton class:

Singleton is over-used.

The pattern goal here is called "Encapsulating Construction". That's where
class X does not care who creates object Y, so long as class X doesn't.

Passing object Y into methods of class X is a less heinous way to keep Y
flexible than making Y a member of X, or a global object.

Don't create a Singleton just because "the program only has one of them".
They are very special beasts, and have the distinguished honor of being the
most abused pattern from /Design Patterns/.
 
D

David Hilsee

Eric said:
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?

What do you want? A factory that instantiates the Singleton object? A
factory that is a Singleton? If you want an instance of a factory class
that is responsible for creating the instance of the Singleton, then just
write a separate class for that. If you want a Singleton factory object,
then don't try to use it to create the instance of the Singleton.
 
E

Eric

David Hilsee said:
What do you want? A factory that instantiates the Singleton object?
Yes.
A factory that is a Singleton?

Possibly but not necessarily.
If you want an instance of a factory class
that is responsible for creating the instance of the Singleton, then just
write a separate class for that. If you want a Singleton factory object,
then don't try to use it to create the instance of the Singleton.

I don't think you understand my question. The Factory Method pattern,
or "virtual constructor" as it's more commonly known in C++, is simply
an interface for creating instances of derived classes without knowing
the actual type of the derived class. The Singleton pattern is a
class that guarantees that only one instance is ever instantiated and
that instance is globally accessible. What I'm asking is, can a
"virtual constructor" create an instance of a derived class that is
itself a Singleton class.

After reading the last part of the Object Factories section in "Modern
C++ Design", I believe the answer is yes if you use Alexandrescu's
approach and break the patterns out of the class into separate
template classes. The example he presents is this:

template SingletonHolder
<
Factory
<
ShapeFactory;

This is a singleton factory of Shapes. If you reverse SingletonHolder
and Factory, you get a factory of Shape singletons:

template Factory
<
SingletonShapeFactory;

Eric.
 
D

Dave Townsend

Eric,

I guess you could have a factory which creates singletons which
are derived from a common base class:

class SingletonFactory
{

public:
SingletonBase* createSingleton( SingletonEnum e )
{

switch(e)
{
case foo: return Foo::getInstance();
case bar:: return Bar::getInstance();

//// etc.
}
}


};

where Foo and Bar are singleton classes derived from Singleton...

Normally in a factory you would have some data to keep track of
the objects you are creating, otherwise you could just as well use
a function.

Does that help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top