Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Singleton with Factory Method possible?

Reply
Thread Tools

Singleton with Factory Method possible?

 
 
Eric
Guest
Posts: n/a
 
      09-21-2004
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-21-2004
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
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      09-21-2004
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


 
Reply With Quote
 
David Hilsee
Guest
Posts: n/a
 
      09-21-2004
"Eric" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.

--
David Hilsee


 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      09-22-2004
"David Hilsee" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Dave Townsend
Guest
Posts: n/a
 
      09-23-2004

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "David Hilsee" <(E-Mail Removed)> wrote in message

news:<(E-Mail Removed)>...
> > 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract factory and Factory pattern C# ASP .Net 4 07-31-2008 03:22 PM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
cool articals on Singleton and Factory Method patterns java touch Java 0 10-28-2006 12:34 PM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM
Abstract Factory or Factory Method pattern question.... Medi Montaseri C++ 17 09-03-2003 06:50 AM



Advertisments