Having finally begun seriously trying to learn some basic patterns (very late in the career, but that's a different story), I'm trying to get my head around the differences between the Factory Pattern and Abstract Factory.

What are the key differences between these two patterns and can you demonstrate a basic implementation of each (or link to an example) in code?

I understand that the Factory Method creates objects through inheritance and Abstract Factory does it through object composition, but from a practical point of view, I'm still having trouble visualising exactly how they each work.

To clarify, do you mean "Factory Method" when you say "Factory Pattern"? If you are talking about the Gang of Four patterns, there is no Factory Pattern, but there are Abstract Factory and Factory Method.
–
Thomas Owens♦Jun 6 '11 at 9:35

1 Answer
1

The Factory Method is usually categorised by a switch statement where each case returns a different class, using the same root interface so that the calling code never needs to make decisions about the implementation.

Think of a credit card validator factory which returns a different validator for each card type.

Is that explanation of Factory method correct? What's about "The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects". So the example is more like Static Factory.
–
SerGOct 15 '14 at 10:21

@SerG Well, in fairness, you've picked up that quote from Wikipedia, on a page that read very differently three years ago. I would argue that the current Wikipedia page contradicts itself in several places, but I don't have the desire to get involved in picking that apart. What I would concede, in hindsight, is that the example I've provided here is a specific kind of Factory Method, known as the Parameterized Factory Method. But the point about the difference between Factory Method and Abstract Factory holds across all types of Factory Method.
–
pdrOct 15 '14 at 17:41

The same statement as my quote exists in GoF "Design Patterns". And Parameterized FM is also described there.
–
SerGOct 16 '14 at 8:27