1

As I anderstand:

The abstract factory pattern provides an interface for creating a family of objects whereas factory method provides an interface for creating one object.

If this is only difference between these patterns why they are being considered separately?

1

3 Answers 3

4

Having a look back at the definition of these two patterns tells us that

  • Factory Method is used to define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."

  • Abstract Factory is used to provide an interface for creating families of related or dependent objects without specifying their concrete classes.

So what you said is correct. Factory Method concerns with creating (usually) one object, whereas Abstract Factory concerns with several related objects.

But that is NOT all there is. If you look at the second word that I bolded in each intent, you will see there's another difference, in terms of the mechanism each pattern works.

  • Factory Method uses SUBCLASSES and inheritance to delegate the instantiation to the concrete implementation. It means that to create new products, you have to inherit the "creator" class and override the factory method. This factory method, in turn, returns the needed product.

  • On the other hand, Abstract Factory uses OBJECT composition (i.e. the factory) to delegate. You have to make code change in the "product" class, not in the creator class. Specifically, inside the product class, you define several related products (lets say ingredients of the product), each of which can be created using the factory OBJECT (composed in the product class, and passed in runtime). The creator class in this case only creates the abstract product and passes into that product a factory that the product will be using.

To make things a bit confusing, however, the central abstract factory in Abstract Factory usually implements Factory Method. This central factory often defines a series of factory methods for all the ingredients, and delegate the creation of each ingredient to the concrete factories.

Hope it helps. Remember that many design patterns are really similar and in fact they are related, e.g. look at the Decorator pattern and Adapter pattern. Most of the times, the difference between two design patterns lies in their respective intents. A good book about Design Patterns, by the way, that I really love is Head First Design Pattern. And there is a similar question posted here.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer. The key point is that a Factory Method is a method, and an Abstract Factory is an object with one or more Factory Methods on it. My answer to a duplicate question is here: stackoverflow.com/questions/5739611/…
3

The factory method is fixed - you can't change it at runtime.

Abstract factory allows you to create objects with a different factory, which can be selected at runtime, depending on some criteria.

Button button = WinButtonFactory.create(); //will always be a "windows button" Button button = buttonFactory.create(); 

on the 2nd like this can be WinButtonFactory extends ButtonFactory or MacOSXButtonFactory extends ButtonFactory. You can pass one or the other depending on the current OS.

4 Comments

So Abstract factory is not several Factory's Methods in abstract class?
I read: <<Factory Method – Used to implement Abstract Factory.>> javabeat.net/2008/08/abstract-factory-pattern
well, that's incorrect. Factory method is actually not even a GoF pattern. They have "factory" and "abstract factory"
Maybe do you mean Simple Factory, not Factory Method?
0

Abstract factory pattern means having a factory for factories (concrete implementations).

4 Comments

I think i'm right. Refer to this link:oodesign.com/abstract-factory-pattern.html "In other words, the Abstract Factory is a super-factory which creates other factories (Factory of factories)"
What is Abstract Factory Pattern? Provide an interface for creating families of related or dependent objects without specifying their concrete classes. See my link above. Someone of us is not right?But who is?
YOU: Abstract Factory creates I: Abstract Factory provides an interface
Both are right ... Abstract factory pattern involves creating a class which decides which concrete implementation to use. The client uses the Abstract interface and is unaware of the underlying implementation (concrete factory).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.