Search code examples
design-patternsfactoryabstract-factoryfactory-method

What is the difference in case of intent and application between these two Patterns?


Factory and Abstract Factory are both creational patterns.

What is the difference in case of intent and application between these two Patterns?


Solution

  • Many people will feel surprised maybe, but this question is incorrect. If you hear this question during an interview, you need to help the interviewer understand where the confusion is.

    Let's start from the fact that there is no concrete pattern that is called just "Factory". There is pattern that is called "Abstract Factory", and there is pattern that is called "Factory Method".

    So, what does "Factory" mean then? one of the following (all can be considered correct, depending on the scope of the reference):

    • Some people use it as an alias (shortcut) for "Abstract Factory".
    • Some people use it as an alias (shortcut) for "Factory Method".
    • Some people use it as a more general name for all factory/creational patterns. E.g. both "Abstract Factory" and "Factory Method" are Factories.

    And, unfortunately, many people use "Factory" to denote another kind of factory, that creates factory or factories (or their interfaces). Based on their theory:

    Product implements IProduct, which is created by Factory, which implements IFactory, which is created by AbstractFactory.

    To understand how silly this is, let's continue our equation:

    AbstractFactory implements IAbstractFactory, which is created by... AbstractAbstractFactory???

    I hope you see the point. Don't get confused, and please don't invent things that don't exist for reason.

    -

    P.S.: Factory for Products is AbstractFactory, and Factory for Abstract Factories would be just another example of AbstractFactory as well.