Home  /  Questions  /  Question



85   96.4
Dec 10, 2010


GoF Design Patterns 4.0 pdf question #2

This is in regards to Factory Method.

The PDF states (page 16)

"So, when do you know the Factory Method is at work? The rules are:

- the method creates a new object

- the method returns an abstract class or interface

- the abstract class or interface is implemented by several classes "

So, it seems that for a method that creates a class, for it to be a Factory Method, it should return an abstract class or interface, right?  Seems clear, but I'm kinda wondering the difference between Factory Method and Abstract Factory.  Fortunately this is answered on page 17:

"This is exactly where Abstract Factory and Factory Method differ: Abstract Factory methods are virtual or abstract and return abstract classes or interfaces. Factory Methods are abstract and return class types. "

It seems this conflicts with the statement that "Factory Methods return abstract classes or interface" when it this says "Factory Methods are abstract and return class types".

Anybody have an explanation?

Thanks




830   99.9
Dec 12, 2010
The difference between Abstract Factory and Factory Method has been blurred over the years.  If you look at differences between the original GOF UML diagrams of Abstract Factory and Factory Method you see in Abstract Factory ´a higher level of abstraction´ (there are several abstract types involved, hence the name Abstract Factory). 

To alleviate any confusion, we should probably remove the sentence:  "This is exactly where Abstract Factory and Factory Method differ: Abstract Factory methods are virtual or abstract and return abstract classes or interfaces. Factory Methods are abstract and return class types. "

The sentence is generally true, but there are most likely edge cases where things are done differently. Also, it would probably be safer to state that methods in Factories (both types) return classes, abstract classes, or interfaces. This would cover all possible factory implementations.