Factory vs Abstract Factory Design Pattern

by BreakHead   Last Updated July 12, 2019 08:26 AM

I was looking at design pattern, but got confused between Factory and Abstract Factory Pattern. will any one please explain the differences when to follow which pattern.

Thanx



Answers 4


The abstract factory doesn't build anything. It just choose the right factory and make it build something.

From the outside it looks like a standard factory and can be used the same way.

It allow you to build object with severals stages in the process.

deadalnix
deadalnix
June 20, 2011 10:40 AM

The factory pattern "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses".

Where as abstract factory pattern "Provide an interface for creating families of related or dependent objects without specifying their concrete classes".

You can think of abstract pattern as factory of factories. As @deadalnix said, it lets you choose the right factory. for more details, read the following links:

  1. Factory Pattern on Wikipedia
  2. Abstract Factory Pattern on Wikipedia

The Factory Method pattern is a method on a class, used to build other instances. You can implement a Factory Method on just about any class that happens to be handy at the time.

The Abstract Factory pattern is about creating a class that has nothing (of interest) other than Factory Method(s) on it. Use abstract factory when you need to create a suite of different but matching objects, or if the act of controlling object creation rises to the point of being the primary function of the class.

matrix
matrix
June 20, 2011 10:48 AM

An Abstract Factory is aimed at creating groups of related objects. The interface of an Abstract Factory will typically contain a number of Factory Methods, one for each type of object to be created.

Here's an example of an Abstract Factory:

public abstract class AnimalFactory
{
    public abstract Animal CreateFish();
    public abstract Animal CreateBird();
    public abstract Animal CreateMammal();
}

public class AfricanAnimalFactory : AnimalFactory
{
    public override Animal CreateFish()
    {
        return new Reedfish();
    }

    public override Animal CreateBird();
    {
        return new Flamingo();
    }

    public override Animal CreateMammal();
    {
        return new Lion();
    }
}

Related resources:

Enrico Campidoglio
Enrico Campidoglio
June 20, 2011 10:54 AM

Real Life Example. (Easy to remember)

Factory

Imagine you are constructing a house and you approach a carpenter for a door. You give the measurement for the door and your requirements, and he will construct a door for you. In this case, the carpenter is a factory of doors. Your specifications are inputs for the factory, and the door is the output or product from the factory.

Abstract Factory

Now, consider the same example of the door. You can go to a carpenter, or you can go to a plastic door shop or a PVC shop. All of them are door factories. Based on the situation, you decide what kind of factory you need to approach. This is like an Abstract Factory.

http://www.dofactory.com/topic/1590/factory-pattern-vs-abstract-factory-pattern.aspx

Debendra Dash
Debendra Dash
January 15, 2018 11:05 AM

Related Questions


Updated June 01, 2019 17:26 PM

Updated May 12, 2018 07:26 AM

Updated September 02, 2017 18:26 PM

Updated November 18, 2017 02:26 AM

Updated May 01, 2016 08:11 AM