Re: passing a Factory to a method to create a generic instance
Tom Anderson wrote:
Except it isn't, because the constructor doesn't have the information it
needs to create the object. Yes, it allocates memory for it and clears
its fields and runs the instance initialiser, but that's only creation
in a trivial sense. If i do Room.class.newInstance(), i have a Room
object, but i don't have a room - it doesn't have a number, a floor, or
a number of beds.
I know that in correct use, you'll go straight from instantiation to
initialisation, so the period of Room-that-is-not-a-room is vanishingly
small, and no danger can occur. The problem arises when the use is
incorrect, and uninitialised Rooms can escape to unwitting code. In our
discussion of operator overloading, you said "I like to design things
that can't fail" - where failure is completely impossible by design.
That's what i'm trying to get at here.
Where one absolutely must separate building an object from its construction,
I'm aware of two patterns, the Builder pattern (think StringBuilder), often
combined with the Factory pattern, and the WhatchaMaCallIt pattern - throw
IllegalStateException for any attempt to operate an unbuilt object.
The objections to every pattern so far proffered amount to the same thing.
There arise scenarios where initialization is just done well after
construction, and those scenarios carry risk that must be managed through
extra code and effort. Properly architected, that effort comes at the root of
the difficulty and not patched onto the surface.
Constructors really work best when limited to "only creation in a trivial
sense". It turns out that jamming all the building phase into construction
carries risk, too, and that risk requires management through code and effort.
Unfortunately, many programs do not evince that care, and do things like
call overridable methods in the constructor of a class that is intended as a
base class, or create side effects from the constructor that are hard to
unroll if construction fails.
I suspect it is easier to manage the risk of separate construction and build
phases than it is the risk of monolithic construction.
--
Lew