Re: Difference between Factory and Abstract Factory



On Mar 26, 1:47 am, EvilOld...@xxxxxxxxxxxxxx wrote:
I've read the gang of four, and various things I've googled, but I'm
just too stupid to really get the difference between a factory and an
abstract factory.
Help me please...

Hi,

Hopefully I'm not too late?

If this next para confuses you please ignore it, after it I have a
simple example but advanced readers ought to understand:

The difference between them is the same as the difference between
Strategy and Template Method, these are like Abstract Factory and
Factory Method respectively. In fact Abstract Factory and Factory
Method are just Strategy and Template Method patterns limited to the
sole purpose of object creation.

Please bare with me a little for this simple example.

CASE 1: ABSTRACT FACTORY

class Blah () {
public Gizmo createGizmo (AbstractFactory factory) {
/* Notice the factory was passed in and you make calls against the
factory like factory.createXyz() ... */
a = factory.createThing();
b = factory.createStuff();
c= connect(a, b);
...
return new Gizmo(a,b,c)
}

// A factory method. Override as required
Thing createThing () {
return new Thing()
}
}

SPECIAL STUFF or SUB-TYPES WANTED
1. You *extend AbstractFactory*
2. Override methods like createThing() *in the factory*.


CASE 2: VERSUS FACTORY METHOD
class Blah2 () {
protected Gizmo createGizmo() {
/* Notice there is no factory, you call factory methods */
a = createThing();
b = createStuff();
c = connect(a,b)
...
return new Gizmo(a,b,c)
}

// Factory methods
protected Thing createThing() {
return new Thing();
}
protected Stuff createStuff() {
return new Stuff();
}

}

SPECIAL STUFF WANTED
1. This time you *extend Blah2 and not AbstractFactory*
2. Override methods like createThing() in Blah2

class FancyBlah extends Blah2 {
/* Notice we are now inheriting Blah2 (not AbstractFactory)
protected Thing createThing() {
return new ThingThatGoesBoing();
}
}


CONCLUSION
What's the difference? One extends the AbstractFactory but in case 2
you extend the class that contains the factory methods, in our case
Blah2. What you inherit or extend makes a big difference, especially
if you have alternate inheritance plans or limitations.

If you draw an inheritance hierarchy tree the picture would be
different.

Check GoF, I think it was, pages 94 and 114 and observe what inherits
what.


REMARK
None of this is to be confused with "parametized factory methods" like

Product createProduct(ProductType type) {
switch(type) {
case PIG: return new Pig();
case DOG: return new Dog();
...
}

This is just a method that knows how to create different kind of
objects, it can appear in either an Abstract Factory or Factory Method
pattern so it cannot be used to differentiate between the two.

Hope that helps,
Cheers.

.



Relevant Pages

  • Re: using jMock with components
    ... >> chance to mock it. ... Whilst running normally the factory class returns ... > but don't worry about Abstract factory it is a valid approach. ... >> I created a factory interface which exposed the components I needed to ...
    (comp.lang.java.programmer)
  • Re: Difference between Factory and Abstract Factory
    ... abstract factory. ... Cat * MakeTheKittah{ ... An Abstract Factory as far as I can see is simply a factory method ...
    (comp.object)
  • Re: using jMock with components
    ... Whilst running normally the factory class returns ... Usually its fine to have the createXXXComponent method on the abstract factory itself, which internally just delegates to the concrete factory. ... The reason I created the two factories is so I can omit the mock factory when I make a distribution. ... with the interface and merge the abstract and real factory classes together and create a getComponentmethod which creates a newInstance of the class using reflection, and have the test factory override that method. ...
    (comp.lang.java.programmer)
  • Re: Difference between Factory and Abstract Factory
    ... This is my take on the Abstract Factory. ... I was too lazy to create a concrete factory for every day of the week, ... public string GetDescription() ... class SaturdayMenu: IMenu ...
    (comp.object)
  • Re: abstract factory -- a doubt ?
    ... Factory does prevent violations of OCP. ... Abstract Factory... ... classes which use Tanks and APCs. ...
    (comp.object)