Abstract Factory Pattern vs Factory Method Pattern

  • the main difference is that the factory method is a method, and an abstract factory is an object
    • the intended purpose of the class containing a factory method is NOT to create objects
    • the intended purpose of an abstract factory should ONLY be used to create objects
  • abstract factory is implemented by composition, but factory method is implemented by Inheritance
  • goals:
    • abstract factory’s goal is to create a family of objects that are designed to work together
    • factory method’s goal is to encapsulate object creation in a separate method, allowing subclasses to provide the implementation for creating specific objects
  • one should take care when using factory methods since it’s easy to break the Liskov Substitution Principle (LSP) when creating objects