sota_333ad4b72095606ab40c

Sota

Posted on November 15, 2024

Factory Pattern

What is Factory pattern?

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.

When to use it?

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)

Problem

If you don't know about Simple Factory, I recommend to study it beforehand. There are plenty of resources but my blog is here.

Image description

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;
    }
}

Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Solution

Image description

  1. 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.

  2. BurgerShop subclasses
    These concrete BurgerShops are responsible for creating concrete burgers. Each subclass that extends BurgerShop defines its own implementation for createBurger().

  3. Burger
    This abstract class provides common interface among all the burgers and defines default behaviors.

  4. Burger subclasses
    Here are our concrete products. They can implement specific behavior by overriding methods as long as they extends Burger class.

Structure

Image description

Implementation in Java

public enum BurgerType {
    BEEF,
    CHICKEN,
    FISH,
    VEGGIE
}
Enter fullscreen mode Exit fullscreen mode
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");
    }
}
Enter fullscreen mode Exit fullscreen mode
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);
    }
}
Enter fullscreen mode Exit fullscreen mode
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);
    }
}
Enter fullscreen mode Exit fullscreen mode
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");
    }
}
Enter fullscreen mode Exit fullscreen mode
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");
    }
}
Enter fullscreen mode Exit fullscreen mode
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);
}
Enter fullscreen mode Exit fullscreen mode
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");
        };
    }
}
Enter fullscreen mode Exit fullscreen mode
public class SeaSideBurgerShop extends BurgerShop {

    @Override
    public Burger createBurger(BurgerType type) {
        return switch (type) {
            case BEEF -> new SeaSideStyleBeefBurger();
            case FISH -> new SeaSideStyleFishBurger();
            default -> throw new IllegalArgumentException("unknown seaside burger type");
        };
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Preparing a bun
Grill a BEEF patty
Add lettuce
Add pickle slices
Add tomato slice
Add BBQ sauce
Wrap a burger up
com.factories.factory.product.CityStyleBeefBurger@3d494fbf
**********************
Preparing a bun
Grill a BEEF patty
Add lettuce
Add pickle slices
Add tomato slice
Add salty sauce
Wrap with a paper with nice sea scenery
com.factories.factory.product.SeaSideStyleBeefBurger@7cd84586
Enter fullscreen mode Exit fullscreen mode

Pitfalls

  • Complex to implement because we need to create lots of classes that extends abstract creator or abstract product.

Comparison with Simple factory

  • In Simple factory, there is typically one factory class to decide which type of product to create, while Factory pattern may introduce multiple factories.
  • Simple factory often uses static method to create objects, which makes it easy to call but hard to extend. On the other hand, Factory method uses abstract method in super class, which acts as interface for all the factories and subclasses will provide concrete implementation for object instantiation.

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 :)

💖 💪 🙅 🚩
sota_333ad4b72095606ab40c
Sota

Posted on November 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Factory Pattern
java Factory Pattern

November 15, 2024