By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,587 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,587 IT Pros & Developers. It's quick & easy.

Singleton with Factory Method possible?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Eric wrote:
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
Jul 22 '05 #2

P: n/a
Eric wrote:
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/.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3

P: n/a
"David Hilsee" <da*************@yahoo.com> wrote in message news:<rJ********************@comcast.com>...
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
<
Shape, std::string, Functor<Shape*>
ShapeFactory;
This is a singleton factory of Shapes. If you reverse SingletonHolder
and Factory, you get a factory of Shape singletons:

template Factory
<
SingletonHolder<Shape>, std::string, Functor<Shape*> SingletonShapeFactory;


Eric.
Jul 22 '05 #4

P: n/a

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?

"Eric" <er**@lemings.com> wrote in message
news:2e**************************@posting.google.c om...
"David Hilsee" <da*************@yahoo.com> wrote in message

news:<rJ********************@comcast.com>...
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
<
Shape, std::string, Functor<Shape*>
>
> ShapeFactory;


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

template Factory
<
SingletonHolder<Shape>, std::string, Functor<Shape*>
> SingletonShapeFactory;


Eric.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.