Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2008-02-29 14:47:07


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


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