Re: Difference between Factory and Abstract Factory
- From: "Alvin Ryder" <alvin321@xxxxxxxxxxx>
- Date: 29 Mar 2007 18:40:45 -0700
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.
.
- References:
- Difference between Factory and Abstract Factory
- From: EvilOldGit
- Difference between Factory and Abstract Factory
- Prev by Date: Re: Double Dispatch Problem: Mobile Creatures and Projectiles in a Game World
- Next by Date: Re: OO and Extending (Was: Is Procedural Paradigm a basis of OO Paradigm?)
- Previous by thread: Re: Difference between Factory and Abstract Factory
- Next by thread: Long Life Objects
- Index(es):
Relevant Pages
|