Singleton with Factory Method possible?

Discussion in 'C++' started by Eric, Sep 21, 2004.

  1. Eric

    Eric Guest

    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.
     
    Eric, Sep 21, 2004
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Sep 21, 2004
    #2
    1. Advertising

  3. Eric

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Sep 21, 2004
    #3
  4. Eric

    David Hilsee Guest

    "Eric" <> wrote in message
    news:...
    > 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
     
    David Hilsee, Sep 22, 2004
    #4
  5. Eric

    Eric Guest

    "David Hilsee" <> wrote in message news:<>...
    > 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.
     
    Eric, Sep 22, 2004
    #5
  6. 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" <> wrote in message
    news:...
    > "David Hilsee" <> wrote in message

    news:<>...
    > > 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.
     
    Dave Townsend, Sep 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Medi Montaseri
    Replies:
    17
    Views:
    879
    Medi Montaseri
    Sep 3, 2003
  2. java touch
    Replies:
    0
    Views:
    336
    java touch
    Oct 28, 2006
  3. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,259
    Proton Projects - Moin
    Mar 27, 2007
  4. C#
    Replies:
    4
    Views:
    413
  5. Wilhelm
    Replies:
    1
    Views:
    167
Loading...

Share This Page