DevOps Zone is brought to you in partnership with:

A software professional. Passionate about java and software development, and watercolor painting Vishal has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

Factory Method vs Abstract Factory (again?)

03.17.2013
| 7200 views |
  • submit to reddit

Hello Friends,

Recently I was asked by one of my friends what is the difference between Factory Method and Abstract Factory design patterns, but I didn't seem to convince him easily. Of course I’d already read Head First, GoF, Pattern Hatching, Refactoring to Patterns, but not deeply all of them and it was not recent. So I decided to explore more and tried to simply the differences, and here ‘s my attempt.

I assume that you are already familiar with both the patterns, so I’ll focus here on where the most people have confusion with the differences. Let’s revisit the definitions and their structure first:

Factory Method: 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: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

AF_DP

Differences

One product vs Set of many

Picture1

This is perhaps the easiest one but important certainly. Factory Method is used to create one product only but Abstract Factory is about creating families of related or dependent products.

Inheritance vs Composition

Picture2

This is perhaps the most confusing one (as both seems to be using inheritance). Factory Method depends on inheritance to decide which product to be created. In Creator class (in structure diagram), it has other methods also (implemented, to manipulate the product) which use the only abstract method FactoryMethod() to create the product and it can only be implemented/changed by subclasses.

Here the Creator class is also acting like Client which depends only on a single method to create a product. We have to create a subclass of whole Creator/Client class to create a new different Product. There’s no separate and dedicated class for creation of a Product, had it been the case we could have used it with composition where we can pass an object of factory to client and client can use it without getting into inheritance hierarchy.

On the other side, in Abstract Factory, there’s a separate class dedicated to create a family of related/dependent Products and its (any concrete subclass factory) object can be passed to the client which uses it (composition). Here the Client gets a different object (concrete factory) to create the Products, instead of creating itself (e.g. using factoryMethod() and forcing inheritance), and thus uses composition.

If we think of just a product creation facility and the client that uses it, it is clear that in Factory Method, we are restricted to use inheritance(class based) and in Abstract Factory we have the flexibility of composition (object based) to create specific Products.

//Factory Method
 class Client {
   public void anOperation() {
     Product p = factoryMethod();
     p.doSomething();
   }
   protected Product factoryMethod() {//or it can be abstract as well
     return new DefaultProduct();
   }
 }
 class NewClient extends Client {
   protected Product factoryMethod() {//overriding
     return new SpecificProduct();
   }
 }
 //Abstract Factory
 class Client {
   private Factory factory;
   public Client (Factory factory) {
     this.factory = factory;
   } 
   public void anOperation() {
     ProductA p = factory.createProductA();
     p.doSomething();//other products and operations as well
   }
 }
 interface Factory {
   ProductA createProductA();
   ProductB createProductB();
 }
 //concrete factories also, implementing Factory interface
 

Method vs full class (object)

Picture3

Factory Method is just a method while Abstract Factory is an object. The purpose of a Class having factory method is not just create objects, it does other work also, only a method is responsible for creating object. In Abstract Factory, the whole purpose of the class is to create family of objects.

Level of abstraction

level-of-abstraction-640x390

Abstract Factory is one level higher in abstraction than Factory Method. Factory Method abstracts the way objects are created, while Abstract Factory also abstracts the way factories are created which in turn abstracts the way objects are created.

One inside another

Picture4

As Abstract Factory is at higher level in abstraction, it often uses Factory Method to create the products in factories.

I also believe that we should not be obsessed with design patterns, these are all built on good basic design principles, and often mixed while using in real world.

I hope it helps somebody else also. Let me also know if it can be improved.

Happy patterns, bye.

Published at DZone with permission of its author, Vishal Jain.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mustafa Asif replied on Fri, 2013/11/08 - 4:44am

In your example of Factory please let your Client class implement Creator

Good description - I have come here after reading many articles and this one satisfies !

Vishal Jain replied on Fri, 2013/11/08 - 9:25am in response to: Mustafa Asif

Thanks Asif, 

Note that NewClient (as a ConcreteCreator) overrides the factory method and as specified in comment, the factory method in Client class (as a Creator) can be abstract as well.

Mustafa Asif replied on Fri, 2013/11/08 - 12:53pm in response to: Vishal Jain

OK, So your Client (class) is the factory itself. Right?

Vishal Jain replied on Sat, 2013/11/09 - 11:23am in response to: Mustafa Asif

Yes

Mustafa Asif replied on Thu, 2013/12/12 - 1:11pm

Hi Vishal,

There are many sites that say Abstract Factory pattern is a factory-factory i.e. they produce factories while there are others that say Abstract Factories use Factory Methods to create objects (I assume this means they create objects/products directly and not create Factories that in turn create objects/products).

For example:

DocumentBuilderFactory abstractFactory = DocumentBuilderFactory.newInstance();

Here DocumentBuilderFactory is described as Abstract Factory because it produces another factory object that is used to create products.

Where as in your example above - it directly created ProductA() etc.

I am confused. What is the actual duty of AbstractFactory - to create a Factory or to create products directly? Can you please elaborate?

Thanks in advance,

Asif

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.