473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

The ideal factory

Hello all,

I'd like to find a way to inject N overloads of a static member function
named Create() into a class foo. The number and type of the parameters of
these overloads will be fixed at compile time but should be able to be
anything. The return values of these overloads will be foo * (later, I'll
want to return boost::shared_ptr<>, but for this discussuion, I'll leave it
at raw pointers to keep it on-topic and simpler). Things should work
seamlessly with multiple inheritance so that we never get into an ambiguous
case if multiple Create() static member functions are pulled in from
different bases. The approaches I've tried have bit me in this case.

The Create() static member functions will return a pointer to a foo that has
been newly-constucted on the heap. The parameters to Create() (however many
there are and whatever their types may be) are used as constructor
parameters.

Assume this hierarchy: foo --> base. Here are some examples of desired
usage:

foo *ptr1 = foo::Create();
foo *ptr2 = foo::Create(my_type_1(), 42, my_type_2());
base *ptr3 = foo::Create(12);

Of course, I could manually insert the Create() overloads into foo myself.
But I would like to find a scheme that automates this as much as possible
for me. This will undoubtedly involve things such as templates, tuples,
perhaps a couple of macros (yuck), etc.

1. Are there any other characteristics I should include in my definition of
an ideal factory?

2. What are some of the approaches I should look into that are most likely
to be successful?

Thanks,
Dave
Jul 23 '05 #1
1 1423
Dave wrote:
I'd like to find a way to inject N overloads of a static member
function named Create() into a class foo. The number and type of the
parameters of these overloads will be fixed at compile time but
should be able to be anything. The return values of these overloads
will be foo * (later, I'll want to return boost::shared_ptr<>, but
for this discussuion, I'll leave it at raw pointers to keep it
on-topic and simpler). Things should work seamlessly with multiple
inheritance so that we never get into an ambiguous case if multiple
Create() static member functions are pulled in from different bases.
From different bases of what? You're inserting 'Create' functions to
the 'foo' class, right? How are those functions going to be used in
the derived classes? Are you hoping to somehow override them in the
derived classes?
The approaches I've tried have bit me in this case.
And which approaches were these?
The Create() static member functions will return a pointer to a foo
that has been newly-constucted on the heap. The parameters to
Create() (however many there are and whatever their types may be) are
used as constructor parameters.

Assume this hierarchy: foo --> base. Here are some examples of desired
usage:

foo *ptr1 = foo::Create();
foo *ptr2 = foo::Create(my_type_1(), 42, my_type_2());
base *ptr3 = foo::Create(12);
Looks fine. Supposedly your 'foo' class has at least three constructors,
one default, one with one 'int' (or something convertible from 'int'),
and one with three arguments. Are these constructors private? It would
probably be nice to have them private if you need to always filter the
creation of 'foo' through the same place (foo::Create). What I don't
really understand is the advantage of this approach versus having just
three public constructors...
Of course, I could manually insert the Create() overloads into foo
myself.
....just like you have to do with constructors...
But I would like to find a scheme that automates this as much
as possible for me.
Automate based on what? I can only see something like the macros:

#define DECLARE_CREATE(clazz, t1) \
private: \
clazz(t1); \
public: \
static clazz* Create (t1)

#define DECLARE_CREATE(clazz, t1, t2) \
private: \
clazz(t1, t2); \
public: \
static clazz* Create (t1, t2)

#define DECLARE_CREATE(clazz, t1, t2, t3) \
private: \
clazz(t1, t2, t3); \
public: \
static clazz* Create (t1, t2, t3)

and then just use those:

class foo : public base {
DECLARE_CREATE(foo, int);
DECLARE_CREATE(foo, double, int);
DECLARE_CREATE(foo, string const&);
This will undoubtedly involve things such as
templates, tuples, perhaps a couple of macros (yuck), etc.
Undoubtedly?
1. Are there any other characteristics I should include in my
definition of an ideal factory?
And what definition is that? I've not seen one so far. Perhaps you
should begin with the actual definition.
2. What are some of the approaches I should look into that are most
likely to be successful?


Read "Modern C++ Design", chapter 6, IIRC. Creation Policies, I
think, is the title of that chapter.

V
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
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....
6
by: Johan Bergman | last post by:
Hi, Maybe someone can help me with this one. The following describes a somewhat simplified version of my problem, but I think it will be sufficient. I want to use class factories (virtual...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
4
by: Eric | last post by:
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?...
10
by: Chris Croughton | last post by:
What do people call their factory functions? 'new' is not an option (yes, it can be overloaded but has to return void*). The context is: class MyClass { public: // Factory functions...
6
by: Dave | last post by:
Hello all, Please see my question embedded in comment form below. Thanks, Dave #include <iostream> #include <boost/shared_ptr.hpp>
8
by: deko | last post by:
Which layer should a Factory class go in? DA - Data Access BL - Business Logic UI - User Interface ??
6
by: Nindi | last post by:
5 Files Singleton.h The Singleton Factory.h The factory creating new objects. The Base class of the hierachy stores a typelist identifying the signature of the constructors to be called...
8
by: googlegroups | last post by:
Hi, I need to parse a binary file produced by an embedded system, whose content consists in a set of events laid-out like this: <event 1<data 1<event 2<data 2... <event n<data n> Every...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.