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. 4 8186
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
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
"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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: javac |
last post by:
for better or worse, my free time is consumed by two Java series books
from Sun:
Java Platform Performance, 2000, by Wilson and Kesselman
Effective Java, 2001, by Bloch
1.) opinions on these...
|
by: Medi Montaseri |
last post by:
Hi,
Given a collection of similar but not exact entities (or products)
Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern
to provide a common interface to these products....
|
by: FluffyCat |
last post by:
In Febraury - April of 2002 I put together in Java examples of all 23
of the classic "Gang Of Four" design patterns for my website. Partly
I wanted to get a better understanding of those patterns....
|
by: Mark |
last post by:
I have an abstract class, and a set of classes that inherit from my abstract
class. The fact that it is abstract is likely irrelevant. I have a static
factory method in my abstract class that...
|
by: jimryanabao |
last post by:
hello, im trying to solve my problem using factory method but im not
sure if im doing it correctly this is my sample code
class Base {
public:
Base();
virtual ~Base;
virtual void draw();
};
|
by: =?Utf-8?B?RXJpYw==?= |
last post by:
I am using the factory method to solve a problem where a factory can produce
product.
I have a base factory class and a base product class. The problem that I am
having is that for every product...
|
by: r035198x |
last post by:
Ok i've overdone something here.
I thought I'd make a factory of singletons during lunch
package factory;
import java.util.*;
class SingletonFactory {
static Hashtable<Class, Boolean>...
|
by: sumittyagi |
last post by:
Hi all,
I was reading factory method pattern from wikipedia link.
There are some limitations mentioned three limitations mentioned there.
These are all fine. But there is a conclusion...
|
by: Pallav singh |
last post by:
Hi ,
when should i select Factory Method / Prototype Design Pattern
during my design phase ??
as both look similar to me
Thanks in Advance
Thanks
Pallav
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |