Factory pattern is a creational pattern that defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory pattern lets a class defer instantiation to subclasses.
Use Factory pattern when you have "product" inheritance hierarchy and possibly add other products to that. (Product refers to an object that is returned by Factory method)
If you don't know about Simple Factory, I recommend to study it beforehand. There are plenty of resources but my blog is here.
Previously, we introduced Simple factory and we could produce variety of burgers while decoupling object creation from client code. Our burger shop has successfully earning profit and now we want to launch other burger shops in different area.
orderBurger method defines the process to sell a burger for a customer.
// This is our Client public class BurgerShop { public Burger orderBurger(BurgerType type) { // Factory is responsible for object creation Burger burger = SimpleBurgerFactory.createBurger(type); burger.prepareBun(); burger.grillPatty(); burger.addToppings(); burger.wrap(); return burger; } }
This is totally fine, but what if we launch other burger shops? Say we launch "SeaSideBurgerShop", we'll create SeaSideBurgerShop class and define its own orderBurger(). The problem is, they might forget to add toppings or do the process in the wrong order.
Problematic SeaSideBurgerShop:
public class SeaSideBurgerShop { public Burger orderBurger(BurgerType type) { Burger burger = SimpleBurgerFactory.createBurger(type); burger.prepareBun(); burger.wrap(); // Wrap a burger before grilling a patty?? burger.grillPatty(); // They forget to add toppings!! return burger; } }
To prevent this, we need a framework for our burger shops that define in which order they do the process and what to do, yet still allows things to remain flexible.
BurgerShop
This abstract class has two methods, orderBurger() and createBurger(). orderBurger() defines what to do and in which order the process should be done. This prevents burger shops to forget some process or mess up process order. creatBurger() is abstract method that lets subclasses determine which kind of burger gets made.
BurgerShop subclasses
These concrete BurgerShops are responsible for creating concrete burgers. Each subclass that extends BurgerShop defines its own implementation for createBurger().
Burger
This abstract class provides common interface among all the burgers and defines default behaviors.
Burger subclasses
Here are our concrete products. They can implement specific behavior by overriding methods as long as they extends Burger class.
// This is our Client public class BurgerShop { public Burger orderBurger(BurgerType type) { // Factory is responsible for object creation Burger burger = SimpleBurgerFactory.createBurger(type); burger.prepareBun(); burger.grillPatty(); burger.addToppings(); burger.wrap(); return burger; } }
public class SeaSideBurgerShop { public Burger orderBurger(BurgerType type) { Burger burger = SimpleBurgerFactory.createBurger(type); burger.prepareBun(); burger.wrap(); // Wrap a burger before grilling a patty?? burger.grillPatty(); // They forget to add toppings!! return burger; } }
public enum BurgerType { BEEF, CHICKEN, FISH, VEGGIE }
public abstract class Burger { public BurgerType type; public List<String> toppings = new ArrayList<>(); public void prepareBun() { System.out.println("Preparing a bun"); } public void grillPatty() { if (type == null) { throw new IllegalStateException("pattyType is undefined"); } System.out.println("Grill a " + type + " patty"); } public void addToppings() { for (String item : toppings) { System.out.println("Add " + item); } } public void wrap() { System.out.println("Wrap a burger up"); } }
public class CityStyleBeefBurger extends Burger { public CityStyleBeefBurger() { type = BurgerType.BEEF; List<String> items = List.of("lettuce", "pickle slices", "tomato slice", "BBQ sauce"); toppings.addAll(items); } }
public class CityStyleVeggieBurger extends Burger { public CityStyleVeggieBurger() { type = BurgerType.VEGGIE; List<String> items = List.of("smoked paprika", "garlic chips", "crushed walnuts", "veggie sauce"); toppings.addAll(items); } }
public class SeaSideStyleBeefBurger extends Burger { public SeaSideStyleBeefBurger() { type = BurgerType.BEEF; // Localized toppings for beef burger in seaside area List<String> items = List.of("lettuce", "pickle slices", "tomato slice", "salty sauce"); toppings.addAll(items); } // Uses localized wrapping paper @Override public void wrap() { System.out.println("Wrap with a paper with nice sea scenery"); } }
public class SeaSideStyleFishBurger extends Burger { public SeaSideStyleFishBurger() { type = BurgerType.FISH; // Localized toppings for fish burger in seaside area List<String> items = List.of("red onion slices", "salty sauce", "fried shrimps"); toppings.addAll(items); } // Uses localized wrapping paper @Override public void wrap() { System.out.println("Wrap with a paper with nice sea scenery"); } }
public abstract class BurgerShop { // This method provides a framework for each burger shops to order burgers public Burger orderBurger(BurgerType type) { Burger burger = createBurger(type); burger.prepareBun(); burger.grillPatty(); burger.addToppings(); burger.wrap(); return burger; } // This is our factory method. Subclasses will override this method, // provide their own implementation, determine which kind of burger gets made. public abstract Burger createBurger(BurgerType type); }
Output:
public class CityBurgerShop extends BurgerShop { @Override public Burger createBurger(BurgerType type) { return switch (type) { case BEEF -> new CityStyleBeefBurger(); case VEGGIE -> new CityStyleVeggieBurger(); default -> throw new IllegalArgumentException("unknown city burger type"); }; } }
You can check all the design pattern implementations here.
GitHub Repository
P.S.
I'm new to write tech blog, if you have advice to improve my writing, or have any confusing point, please leave a comment!
Thank you for reading :)
The above is the detailed content of Factory Pattern. For more information, please follow other related articles on the PHP Chinese website!