Boost logo

Boost :

From: Anthony Liguori (anthony_at_[hidden])
Date: 2002-11-21 21:16:46


Christophe Meessen wrote:

> What I need is something like the factory method design pattern. It is
> a method that will construct different types of class instances
> according to some key information. It could be the class name string,
> an integer or anything else. It is trivial to implement using
> Anthony's virtual constructors.

I'm actually looking to produce similiar functionality for my metaclass
library (returns ctors and methods based on a function type and key
information). The thing is, with the virtual ctor library, implementing
a factory merely requires:

// Assuming you want a factory for class Base that takes two int arguments

typedef std::map<std::string, boost::function<Base *(int, int)> >
BaseFactory;
BaseFactory factory;

...

// Register subclasses
factory["Subclass1"] = boost::constructor<Subclass1 *(int, int)>();
factory["Subclass2"] = boost::constructor<Subclass2 *(int, int)>();

...

// Make use of factory
std::shared_ptr<Base> base = factory["Subclass1"](10, 15);

>
> Anyway, if Anthony allows me I will now use his library as the basic
> building block of my object factory

The boost license is Open Source so you don't need my permission :) Out
of curiousity though, do you think that a factory class is still useful?
 Perhaps we can take this discussion off the list and figure out what
the requirements of a factory class would be that virtual constructors
wouldn't provide.

Regards,
Anthony Liguori


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk