Home > Java > javaTutorial > Factory Pattern

Factory Pattern

Linda Hamilton
Release: 2024-11-20 01:01:03
Original
644 people have browsed it

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.

Factory Pattern

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

Copy after login
Copy after login

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;
    }
}
Copy after login
Copy after login

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

Factory Pattern

  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

Factory Pattern

Implementation in Java

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

Copy after login
Copy after login
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;
    }
}
Copy after login
Copy after login
public enum BurgerType {
    BEEF,
    CHICKEN,
    FISH,
    VEGGIE
}
Copy after login
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");
    }
}
Copy after login
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);
    }
}
Copy after login
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);
    }
}
Copy after login
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");
    }
}
Copy after login
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");
    }
}
Copy after login
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);
}
Copy after login

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");
        };
    }
}
Copy after login

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

The above is the detailed content of Factory Pattern. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template