Re: passing a Factory to a method to create a generic instance
Tom Anderson wrote:
With first-class functions, you could even make the CratePacker take a
FUNCTION FROM void TO Widget or whatever it'd be called, and then pass
in a Widget class's constructor. That's what i do in python.
Constructors aren't methods in Java, therefore would not be eligible even if
we did get closures. To my eye, the pluggable (and perhaps anonymous) class
syntax is easier to read and maintain after the fact.
I fear that Java will grow from a WORA (Write Once, Run Anywhere) language to
a WORN (Write Once, Read Never) language.
Lew wrote:
In this simple example, Wdiget should be an interface rather than a
class, but that in no wise detracts from the main point of the example.
Tom:
Widget or WidgetFactory? In this example (or rather, the hypothetical
Both, really, but only because the example is so simple.
expanded real-world case it's a cartoon of), whether Widget is an
interface or a class depends on design considerations related to what
Widgets do, rather than the use of the factory pattern - do they have
code they can share? Even if not, if the main thing that a Bolt is is a
kind of Widget, i'd say use an abstract base class, not an interface,
since it communicates a stronger sense of is-a. WidgetFactory, though,
yes, that could well be an interface.
Leaving aside the extreme and controversial view that one should never use a
base class, a view that I do not hold, BTW, there is no difference in
"communication ... of 'is-a'" between inheriting from a class vs. inheriting
from an interface. Both equally express inheritance. In fact, where one does
use an abstract base class, that abstract base class itself had better
freaking implement an interface that specifies the exact same abstract
methods. So the implementing classes actually will 'be-a' type abstract base
class and also 'be-a' type interface.
The reason to choose a base class like WidgetFactory has absolutely nothing so
idiosyncratic and subjective as whether it "communicates a stronger sense of
is-a". It has to do with whether one should root the implementing classes at
a single point, and whether that point should include a chunk of the
implementation of its own super-interface. These are objective and
non-aesthetically-motivated criteria.
--
Lew