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?

ASD
  • 725
  • 3
  • 10
  • 19
  • 1
    possible duplicate of [What is the basic difference between Factory and Abstract Factory Patterns?](http://stackoverflow.com/questions/1001767/what-is-the-basic-difference-between-factory-and-abstract-factory-patterns) Also see http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern – nawfal Apr 18 '13 at 18:06

3 Answers3

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.

Community
  • 1
  • 1
Son Do Lenh
  • 826
  • 1
  • 8
  • 16
  • 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: http://stackoverflow.com/questions/5739611/differences-between-abstract-factory-pattern-and-factory-method/5740020#5740020 – Tom Dalling May 20 '12 at 09:43
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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

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

sul
  • 267
  • 1
  • 9
  • 1
    I think i'm right. Refer to this link:http://www.oodesign.com/abstract-factory-pattern.html "In other words, the Abstract Factory is a super-factory which creates other factories (Factory of factories)" – sul May 18 '12 at 22:37
  • 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? – ASD May 18 '12 at 22:44
  • YOU: Abstract Factory creates I: Abstract Factory provides an interface – ASD May 18 '12 at 22:45
  • 1
    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). – sul May 18 '12 at 22:51