0% found this document useful (0 votes)
365 views

Differences Between Abstract Factory Pattern and Factory Method - Stack Overflow PDF

The key differences between the Abstract Factory pattern and Factory Method pattern are: 1) The Abstract Factory pattern provides a way to create families of related objects without specifying their concrete classes, while the Factory Method pattern is used to create a single object. 2) With Abstract Factory, object creation is delegated to another object (the factory) via composition, while Factory Method uses inheritance and relies on subclasses to determine the object instantiated. 3) The Abstract Factory interface defines multiple factory methods to create different object types, while the Factory Method is a single method for object creation.

Uploaded by

gopi247
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
365 views

Differences Between Abstract Factory Pattern and Factory Method - Stack Overflow PDF

The key differences between the Abstract Factory pattern and Factory Method pattern are: 1) The Abstract Factory pattern provides a way to create families of related objects without specifying their concrete classes, while the Factory Method pattern is used to create a single object. 2) With Abstract Factory, object creation is delegated to another object (the factory) via composition, while Factory Method uses inheritance and relies on subclasses to determine the object instantiated. 3) The Abstract Factory interface defines multiple factory methods to create different object types, while the Factory Method is a single method for object creation.

Uploaded by

gopi247
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8/14/12Differences between Abstract Factory Pattern and Factory Method - Stack Overflow

Differences between Abstract Factory Pattern and Factory Method

I know there are many posts out there about the differences between these two patterns, but there are a
few things that I cannot find.

From what I have been reading, I see that the factory method pattern allows you to define how to create
a single concrete product but hiding the implementation from the client as they will see a generic product.
My first question is about the abstract factory. Is it's role to allow you to create families of concrete
objects in (that can depend on what specific factory you use) rather than just a single concrete object?
Does the abstract factory only return one very large object or many objects depending on what methods
you call?

My final 2 questions are about a single quote that I cannot fully understand that I have seen in numerous
places:

One difference between the two is that with the Abstract Factory pattern, a class delegates the
responsibility of object instantiation to another object via composition whereas the Factory Method
pattern uses inheritance and relies on a subclass to handle the desired object instantiation.

My understanding is that the factory method pattern has a Creator interface that will make the
ConcreteCreator be in charge of knowing which ConcreteProduct to instantiate. Is this what it means by
using inheritance to handle object instantiation?

Now with regards to that quote, how exactly does the Abstract Factory pattern delegate the responsibility
of object instantiation to another object via composition? What does this mean? It looks like the Abstract
Factory pattern also uses inheritance to do the construction process as well in my eyes, but then again I
am still learning about these patterns.

Any help (especially with the last question of mine) would be greatly appreciated. Thank you!

design-patterns factory-pattern factory-method gof abstract-factory

asked Apr 21 '11 at 4:41


Silverbolt
316 2 9
100% accept rate

feedback

3 Answers

The Difference Between The Two


The main difference between a "factory method" and an "abstract factory" is that the factory method is a
single method, and an abstract factory is an object. I think a lot of people get these two terms confused,
and start using them interchangeably. I remember that I had a hard time finding exactly what the
difference was when I learnt them.

Because the factory method is just a method, it can be overridden in a subclass, hence the second half
of your quote:

... the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object
instantiation.

stackoverflow.com/questions/…/differences-between-abstract-factory-pattern-and-factory-method 1/4
8/14/12Differences between Abstract Factory Pattern and Factory Method - Stack Overflow
The quote assumes that an object is calling it's own factory method here. Therefor the only thing that
could change the return value would be a subclass.

The abstract factory is an object that has multiple factory methods on it. Looking at the first half of your
quote:

... with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to
another object via composition ...

What they're saying is that there is an object A, who wants to make a Foo object. Instead of making the
Foo object itself (e.g. with a factory method), it's going get a different object (the abstract factory) to
create the Foo object.

Code Examples

To show you the difference, here is a factory method in use:

class A {
public void doSomething() {
Foo f = makeFoo();
f.whatever();
}

protected Foo makeFoo() {


return new RegularFoo();
}
}

class B extends A {
protected Foo makeFoo() {
//subclass is overriding the factory method
//to return something different
return new SuperFoo();
}
}

And here is an abstract factory in use:

class A {
private Factory factory;

public A(Factory factory) {


this.factory = factory;
}

public void doSomething() {


//The concrete class of "f" depends on the concrete class
//of the factory passed into the constructor. If you provide a
//different factory, you get a different Foo object.
Foo f = factory.makeFoo();
f.whatever();
}
}

interface Factory {
Foo makeFoo();
Bar makeBar();
Aycufcn makeAmbiguousYetCommonlyUsedFakeClassName();
}

//need to make concrete factories that implement the "Factory" interface here

edited Jul 23 at 2:26 answered Apr 21 '11 at 5:39


Tom Dalling
6,164 11 34

stackoverflow.com/questions/…/differences-between-abstract-factory-pattern-and-factory-method 2/4
8/14/12Differences between Abstract Factory Pattern and Factory Method - Stack Overflow

1 A little something I wrote: butunclebob.com/ArticleS.UncleBob.AbstractFactoryDanielT – Daniel T. Apr 21 '11


at 11:42

Thank you for your feedback.

Abstract factory creates a base class with abstract methods defining methods for the objects that
should be created. Each factory class which derives the base class can create their own implementaton
of each object type.

Factory method is just a simple method used to create objects in a class. It's usually added in the
aggregate root (The Order class has a method called CreateOrderLine)

Summary

stackoverflow.com/questions/…/differences-between-abstract-factory-pattern-and-factory-method 3/4
8/14/12Differences between Abstract Factory Pattern and Factory Method - Stack Overflow
The difference is that the intended purpose of the class containing a factory method is not to create
objects, while an abstract factory should only be used to create objects.

One should take care when using factory methods since it's easy to break the LSP (Liskovs Substitution
principle) when creating objects.

edited Jul 22 at 21:28 answered Apr 21 '11 at 5:48


Firkraag jgauffin
20 5 30.1k 5 38 94

feedback

I think it would be much less confusing if instead of anOperation() it would be written like anOperation,
anOperation2, anOperation3 to stress out more, that there is no additional creator class, but the client of
the created objects uses them right away. Maybe the name should be written like "Creator / Client".

Thanks to Tom Dalin for the best explanation which finally made me understand the pattern.

answered Jul 22 at 21:09


Firkraag
20 5

feedback

Not the answer you're looking for? Browse other questions tagged design-patterns

factory-pattern factory-method gof abstract-factory or ask your own question.

question feed

stackoverflow.com/questions/…/differences-between-abstract-factory-pattern-and-factory-method 4/4

You might also like