|
 |
On Sun, 25 Apr 2010 23:53:50 +0200, Darren New <dne### [at] san rr com> wrote:
> I have noticed that whenever I wind up using the Factory pattern, I wind
> up with a listing of all the classes somewhere in a central place,
> namely the factory method that instantiates the appropriate class. It
> seems to me this kind of defeats the entire purpose of the method.
Decentralization is not the purpose of the Factory pattern. Still, it is
sometimes useful to decouple even the factory from knowledge about the
specific concrete classes, especially if there are many of them.
> Thoughts?
Another way is to make the factory store a dictionary of construction
methods/functors/prototypes, and register classes/objects into the factory
from elsewhere. This also lets you add new classes/objects to the factory
at runtime. In C++, the registration part can be set up to happen
automatically with minimal effort; I am not sure about other languages.
In languages with reflection, you could have the factory look up a class
name, check if the class belongs to the right hierarchy for that factory,
and then simply call a static factory method (or just the default
constructor) defined in that class.
--
FE
Post a reply to this message
|
 |