POV-Ray : Newsgroups : povray.off-topic : The "Factory" Pattern : Re: The "Factory" Pattern Server Time
4 Sep 2024 05:20:24 EDT (-0400)
  Re: The "Factory" Pattern  
From: Fredrik Eriksson
Date: 26 Apr 2010 04:52:40
Message: <op.vbrhl2kp7bxctx@toad.bredbandsbolaget.se>
On Sun, 25 Apr 2010 23:53:50 +0200, Darren New <dne### [at] sanrrcom> 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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.