43

Having finally begun seriously trying to learn some basic patterns (very late in the career, but that's a different story), I'm trying to get my head around the differences between the Factory Pattern and Abstract Factory.

What are the key differences between these two patterns?

I understand that the Factory Method creates objects through inheritance and Abstract Factory does it through object composition, but from a practical point of view, I'm still having trouble visualising exactly how they each work.

6
  • 4
    stackoverflow.com/questions/1001767/…
    – Natrium
    Commented Jun 6, 2011 at 9:32
  • 2
    To clarify, do you mean "Factory Method" when you say "Factory Pattern"? If you are talking about the Gang of Four patterns, there is no Factory Pattern, but there are Abstract Factory and Factory Method.
    – Thomas Owens
    Commented Jun 6, 2011 at 9:35
  • Yes - Factory Method. Commented Jun 6, 2011 at 9:47
  • 3
    To be fair, the two phrases seem to be pretty commonly interchanged. Commented Jun 6, 2011 at 9:48
  • 1
    Ah, Factory Method. A workaround for the fact that new isn't a method (in some – admittedly common – object systems). Commented Jun 6, 2011 at 10:38

1 Answer 1

49

The Factory Method is usually categorised by a switch statement where each case returns a different class, using the same root interface so that the calling code never needs to make decisions about the implementation.

Think of a credit card validator factory which returns a different validator for each card type.

public ICardValidator GetCardValidator (string cardType)
{
    switch (cardType.ToLower())
    {
        case "visa":
            return new VisaCardValidator();
        case "mastercard":
        case "ecmc":
            return new MastercardValidator();
        default:
            throw new CreditCardTypeException("Do not recognise this type");
    }
}

The Abstract Factory is where you have multiple concrete factory classes (not Factory Methods) derived from one interface which may return many different types from different methods.

Think of a chess game manager with a different class for each set of variant rules.

public class StandardChessRulesFactory : IChessRulesFactory
{
    public IBoardMapper GetBoardMapper()
    {
        return new StandardChessBoardMapper();
    }

    public IKingMover GetKingMover()
    {
        return new StandardChessKingMover();
    }

    public IMoveClock GetMoveClock()
    {
        return new StandardMoveClock();
    }
}

public class HexagonalChessRulesFactory : IChessRulesFactory
{
    public IBoardMapper GetBoardMapper()
    {
        return new HexagonalChessBoardMapper();
    }

    public IKingMover GetKingMover()
    {
        return new HexagonalChessKingMover();
    }

    public IMoveClock GetMoveClock()
    {
        return new StandardMoveClock();
    }
}

public class SpeedChessRulesFactory : IChessRulesFactory
{
    public IBoardMapper GetBoardMapper()
    {
        return new StandardChessBoardMapper();
    }

    public IKingMover GetKingMover()
    {
        return new StandardChessKingMover();
    }

    public IMoveClock GetMoveClock()
    {
        return new SpeedChessMoveClock();
    }
}

An Abstract Factory, much like a Strategy, is often selected using a Factory Method, but it isn't necessary to combine them so it is its own pattern.

5
  • 5
    Is that explanation of Factory method correct? What's about "The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects". So the example is more like Static Factory.
    – SerG
    Commented Oct 15, 2014 at 10:21
  • @SerG Well, in fairness, you've picked up that quote from Wikipedia, on a page that read very differently three years ago. I would argue that the current Wikipedia page contradicts itself in several places, but I don't have the desire to get involved in picking that apart. What I would concede, in hindsight, is that the example I've provided here is a specific kind of Factory Method, known as the Parameterized Factory Method. But the point about the difference between Factory Method and Abstract Factory holds across all types of Factory Method.
    – pdr
    Commented Oct 15, 2014 at 17:41
  • 2
    The same statement as my quote exists in GoF "Design Patterns". And Parameterized FM is also described there.
    – SerG
    Commented Oct 16, 2014 at 8:27
  • The important part is that the factory will give the caller a suitable object, depending on the particular situation, and the caller doesn't need to know what exactly the class of that object it, and doesn't need to know how the particular object was chosen, as long as the object supports an interface that the caller knows about.
    – gnasher729
    Commented Jul 20, 2015 at 13:48
  • You should state that clear in your answer that your example is not a typical factory method pattern, but some specialization named parameterized factory method. And write about definition of typical factory method, because it is mislading right now. I was just learning about factory method pattern, I understood everything and then I read that answer which shows factory method pattern as something different and I was confused. There is no information about that example not being typical factory method pattern. Thanks to SerG for poiniting that out in the comment.
    – ctomek
    Commented Feb 28, 2017 at 15:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.