POV-Ray : Newsgroups : povray.off-topic : The "Factory" Pattern : Re: The "Factory" Pattern Server Time
4 Sep 2024 05:16:08 EDT (-0400)
  Re: The "Factory" Pattern  
From: Darren New
Date: 26 Apr 2010 13:10:21
Message: <4bd5c8fd$1@news.povray.org>
Fredrik Eriksson wrote:
> Decentralization is not the purpose of the Factory pattern.

Uh, sure it is, in the same sense that inheritance is decentralization. What 
do *you* think it's for?  Maybe it's good for something else too.

> Still, it is 
> sometimes useful to decouple even the factory from knowledge about the 
> specific concrete classes, especially if there are many of them.

Right.

>> 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.


Ah, now you're talking about implementations in current languages. I was 
talking about a language feature for future languages that would basically 
automate this whole process. Working with a language where it isn't built 
in, thinking about the ways I've used it and seen it used, and trying to 
come up with a way to eliminate the "registration" aspect and basically make 
it seamless, in the same way that exceptions and OO are seamless in C++ but 
you have to do them manually in C.

Plus, that's no longer "New". It's a separate method that returns a pointer. 
I.e., you're still calling a factory method instead of just instantiating a 
class and having it automatically instantiate the right subclass.

Of course, in some languages like Smalltalk (and Python, I think?) "new" is 
just a method that returns a reference, and that reference doesn't have to 
be the same class you invoked "new" on, so in that sense much of the hard 
work is done. The base class would just implement "look up the right 
subclass" process in its constructor and do the factory work there.

Altho, I suppose with MI (so you can inherit the "Factory" methods anywhere) 
and static library initialization (so you can invoke those implicitly to do 
registration when the class gets loaded) you could come very close to doing 
this with nothing but a library.


-- 
Darren New, San Diego CA, USA (PST)
   Linux: Now bringing the quality and usability of
   open source desktop apps to your personal electronics.


Post a reply to this message

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