Abstract Factory Design Pattern is a creational design pattern in which interfaces are defined for creating families of related objects without specifying their actual implementations
The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It's particularly useful when you need to create objects that belong to different product families but should work together.
This implementation demonstrates a restaurant cuisine scenario where different factories create appropriate meal combinations:
- RecipeFactory (Abstract Factory): Defines methods for creating families of related products
- Product Families: Sandwich and Dessert abstract classes
- Concrete Factories: AdultCuisineFactory and KidCuisineFactory
- Concrete Products: BLT, GrilledCheese (sandwiches) and CremeBrulee, IceCreamSundae (desserts)
RecipeFactory- Abstract factory defining CreateSandwich() and CreateDessert() methodsAdultCuisineFactory- Creates BLT sandwich and CremeBrulee dessert for adultsKidCuisineFactory- Creates GrilledCheese sandwich and IceCreamSundae dessert for kidsSandwich- Abstract product class for sandwichesDessert- Abstract product class for dessertsBLT,GrilledCheese- Concrete sandwich implementationsCremeBrulee,IceCreamSundae- Concrete dessert implementations
RecipeFactory factory;
// Based on user input, choose appropriate factory
factory = new AdultCuisineFactory(); // or KidCuisineFactory()
var sandwich = factory.CreateSandwich(); // BLT or GrilledCheese
var dessert = factory.CreateDessert(); // CremeBrulee or IceCreamSundae- Ensures that products from the same family are used together
- Isolates concrete classes from client code
- Makes exchanging product families easy
- Promotes consistency among products in a family