$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: Anthony Liguori (anthony_at_[hidden])
Date: 2003-05-19 20:12:23
>
>
>Is seems to be some unusual implementation of Factory Pattern. Could you
>please elaborate little more here ?
>
>
struct A {
virtual int my_func() {
return 3;
}
};
struct B : public A {
virtual int my_func() {
return 4;
}
};
B b;
A &a = b;
assert(a.my_func() == 4);
In the above exampe, my_func is a virtual method. The caller only needs
to know the interface of the base class and is able to class function of
a class he knows absolutely nothing about. This encapsulation allows a
great number of design patterns to be implemented.
struct A {
A(int, int) {}
};
struct B : public A {
B(int, int) {}
};
boost::function<A *(int, int)> func = boost::ctor_fn<B *(int, int)>();
A *a = func(1, 2);
In this example, func is a virtual constructor. The caller only needs
to know the interface of the base class to create instances of its
subclasses.
The factory pattern a much more specific usage of this concept. The
factory pattern allows for a single class to create any number of
subclasses based on an inputted key. One could use boost::ctor_fn to
implement a factory, but it has many uses outside of factories.
One would use this library any time a class needed to be able to create
objects but didn't know what particular type of object it needed to
create at compile time.
Regards,
Anthony Liguori