BreakHead
BreakHead

Reputation: 10672

Factory vs Abstract Factory Design Pattern

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

Upvotes: 1

Views: 9070

Answers (4)

deadalnix
deadalnix

Reputation: 2325

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.

Upvotes: 3

Debendra Dash
Debendra Dash

Reputation: 5596

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

Upvotes: 2

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59923

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:

Upvotes: 11

matrix
matrix

Reputation: 3080

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.

Upvotes: 6

Related Questions