Abstract Factory Pattern
- is a type of Factory Design Pattern that lets you produce families of related objects without specifying their concrete classes
- an extension of Abstract Factory Pattern is Factory of Factories Pattern
Abstract Factory Pattern - Example Implementations
ConcreteFactories create different subtypes of Products
interface ProductA {} interface ProductB {} interface AbstractFactory { ProductA createProductA(); ProductB createProductB(); }
class ProductA1 implements ProductA {} class ProductB1 implements ProductB {} class AbstractFactory1 implements AbstractFactory { ProductA createProductA() { return new ProductA1(); } ProductB createProductB() { return new ProductB1(); } }class ProductA2 implements ProductA {} class ProductB2 implements ProductB {} class AbstractFactory2 implements AbstractFactory { ProductA createProductA() { return new ProductA2(); } ProductB createProductB() { return new ProductB2(); } }Use case #1 - using AbstractFactory1
public static void main(String[] args) { AbstractFactory abstractFactory = new AbstractFactory1(); ProductA productA = abstractFactory.createProductA(); ProductB productB = abstractFactory.createProductB(); }Use case #2 - using AbstractFactory2
public static void main(String[] args) { AbstractFactory abstractFactory = new AbstractFactory2(); ProductA productA = abstractFactory.createProductA(); ProductB productB = abstractFactory.createProductB(); }
ConcreteFactories create same Products but with different states
class ProductA {} class ProductB {} interface AbstractFactory { ProductA createProductA(); ProductB createProductB(); }class ProductA1 implements ProductA {} class ProductB1 implements ProductB {} class AbstractFactory1 implements AbstractFactory { ProductA createProductA() { ProductA productA = new ProductA(); // custom logic return productA; } ProductB createProductB() { ProductB productB = new ProductB(); // custom logic return productB; } }class ProductA2 implements ProductA {} class ProductB2 implements ProductB {} class AbstractFactory2 implements AbstractFactory { ProductA createProductA() { ProductA productA = new ProductA(); // custom logic return productA; } ProductB createProductB() { ProductB productB = new ProductB(); // custom logic return productB; } }Use case #1 - using AbstractFactory1
public static void main(String[] args) { AbstractFactory abstractFactory = new AbstractFactory1(); ProductA productA = abstractFactory.createProductA(); ProductB productB = abstractFactory.createProductB(); }Use case #2 - using AbstractFactory2
public static void main(String[] args) { AbstractFactory abstractFactory = new AbstractFactory2(); ProductA productA = abstractFactory.createProductA(); ProductB productB = abstractFactory.createProductB(); }
Relation to Other Patterns
Click here to expand...
Link to originalAbstract Factory Pattern vs Facade Pattern
- Abstract Factory can be used as an alternative to Facade to hide platform-specific classes
Link to originalAbstract 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
Link to originalAbstract Factory Pattern vs Factory of Factory Pattern
- Factories of Factories is an extension/wrapper around multiple Abstract Factories
/creational-design-patterns/factory-design-patterns/abstract-factory-pattern/abstract-factory-design-pattern.png)