Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 and can you demonstrate a basic implementation of each (or link to an example) in code?

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.

share|improve this question
4  
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 Jun 6 '11 at 9:35
    
Yes - Factory Method. –  Phil.Wheeler Jun 6 '11 at 9:47
2  
To be fair, the two phrases seem to be pretty commonly interchanged. –  Phil.Wheeler Jun 6 '11 at 9:48
    
Ah, Factory Method. A workaround for the fact that new isn't a method (in some – admittedly common – object systems). –  Donal Fellows Jun 6 '11 at 10:38

1 Answer 1

up vote 27 down vote accepted

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.

share|improve this answer
    
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 Oct 15 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 Oct 15 at 17:41
    
The same statement as my quote exists in GoF "Design Patterns". And Parameterized FM is also described there. –  SerG Oct 16 at 8:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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