Ed Thompson <> wrote in message news:<zb%Ob.245605$. rr.com>...
> > Your suggestion is hardly a factory method.
> I was suggesting it was an implmentation of the factory pattern, not a
> factory method per se.
>
There really are two types of factory patterns per se GOF - factory
method and abstract factory.
As I understand it, you use 'factory method' when the invoker of the
factory knows which class he wants. There is one common abstract class
for all classes requested (the return value of getClass(String
identifier) ) . The implementation can get done with static ints, or
sometimes pass the name of the class to be used via
classForName.newInstance() .
Abstract factory differs in that the getClass() method returns a
factory itself - hence the name abstract factory. Here, typically the
factory decides which factory to return. Or alternatively, there is a
getFactoryType1() and getFactoryType2() methods.
> > you are starting to mix code for how to make calculators with their functional
> > code.
>
> True, but I believe I found a precedent in the java.util.Calendar class,
> whose getInstance() method seems to be doing something similiar. Is
> this different?
>
> > Plus it means the base class has to have code relating to its derived
> > classes.
>
> True. Feedback on why this is bad?
Tight coupling. One of the ideas of patterns is to encapsulate change.
Changing one part and affecting the other part should be avoided.
Also, getInstance() virtually always means a Singleton - another
pattern altogether. Singletons are a generator of a single class,
typically with expensive network parameters that are always the same
and you want only one way to do it, versus factories which can return
several different classes of an abstract type.
Hope that helps,
iksrazal
|