functional abstract factory or virtual constructor

Boost Users/Developers, I'm writing to gauge interest in an 'abstract factory', very similar to the recently accepted functional/factory. I realise there has been interest in this idea in the past (ctor_fn), but nothing seems to have come of it (that I am aware of). Does anything similar exist? Example code: // // Given an inheritance hierarchy where the base class // constructor has a certain signature... // struct A { A(const string&, int){} virtual ~A(){} }; struct B : public A { B(const string& s, int i) : A(s, i) {} virtual ~B(){} }; // // typedef some factories... // typedef abstract_factory<A*(const string&, int)> AFactory; typedef concrete_factory<AFactory, B*, MyAllocator> BFactory; // // Use them... // AFactory* factory = new BFactory; A* a = (*factory)("a string", 1); // Look ma, temporaries. assert(dynamic_cast<B*>(a)); vector<string> v1; vector<A*> v2; transform(v1.begin(), v1.end(), back_inserter(v2), bind<A*>(ref(*factory), _1, 1234)); The naming of 'abstract_factory' and 'concrete_factory' is reminiscent of the Loki AbstractFactory and ConcreteFactory, as is the idea of passing the abstract_factory as a template parameter into concrete_factory. I'm new to this mailing list as well, so I'm not aware of all previous conversation on this topic. What do you think? Stathi

Stathie Triadis wrote:
Boost Users/Developers,
I'm writing to gauge interest in an 'abstract factory', very similar to the recently accepted functional/factory.
[...]
The naming of 'abstract_factory' and 'concrete_factory' is reminiscent of the Loki AbstractFactory and ConcreteFactory, as is the idea of passing the abstract_factory as a template parameter into concrete_factory.
I'm new to this mailing list as well, so I'm not aware of all previous conversation on this topic.
What do you think?
Why not simply use factory/function combination? typedef boost::function< A*(string, int) > AFactory; typedef boost::factory< B* > BFactory; // AFactory f = BFactory(); A* a_B = f("foo",123); assert(dynamic_cast<B*>(a_B)); Please note that in this case, BFactory is the "concrete factory" but it does not know about that role. It's "virtualized from the outside". Also note that Boost.Function is optimized to avoid a huge amount of (relative) space overhead for RTTI we otherwise get as a result of encapsulating a single polymorphic function with polymorphic classes. For the transform you might get away without the dynamic call (that is if you don't need type erasure on the factory for modularization): vector<string> v1; vector<A*> v2; transform(v1.begin(), v1.end(), back_inserter(v2), bind(BFactory(), _1, 1234)); // note construction can be inlined into the loop Regards, Tobias

On Fri, Feb 29, 2008 at 7:47 PM, Tobias Schwinger <tschwinger@isonews2.com> wrote:
I'm writing to gauge interest in an 'abstract factory', very similar to the recently accepted functional/factory.
Why not simply use factory/function combination?
typedef boost::function< A*(string, int) > AFactory; typedef boost::factory< B* > BFactory;
That's quite nice... I didn't know you could do that. But this is no good for modularization, correct? Please note that in this case, BFactory is the "concrete factory" but it
does not know about that role. It's "virtualized from the outside".
[...]
For the transform you might get away without the dynamic call (that is if you don't need type erasure on the factory for modularization):
This was actually half the motivation for creating abstract_factory. I needed a way of reducing cyclic linker dependencies (VC++). A simple way of doing that is to virtualize methods, but of course you can't do that for constructors. I needed a virtual constructor of some sort. The other reason came from working with the Loki::AbstractFactory but becoming frustrated with not being able to pass parameters into the Create method. abstract_factory can be used a a component to create a Loki::AbstractFactory without this limitation. I've put abstract_factory.hpp in the vault if you'd like to take a look, along with an example that uses Loki::AbstractFactory, here: http://www.boost-consulting.com/vault/index.php?action=downloadfile&filename=abstract_factory.zip&directory=& Cheers, Stathi

Stathi Triadis wrote:
On Fri, Feb 29, 2008 at 7:47 PM, Tobias Schwinger <tschwinger@isonews2.com> wrote:
I'm writing to gauge interest in an 'abstract factory', very similar to the recently accepted functional/factory. Why not simply use factory/function combination?
typedef boost::function< A*(string, int) > AFactory; typedef boost::factory< B* > BFactory;
That's quite nice... I didn't know you could do that. But this is no good for modularization, correct?
Not that I'm aware of. Why do you think so? Doesn't the OO-style abstract factory you use in fact just encapsulate a function? Regards, Tobias
participants (3)
-
Stathi Triadis
-
Stathie Triadis
-
Tobias Schwinger