Simple Factory Design Pattern and How to Implement It in Java

In this blog post we’ll first cover what a simple factory design pattern is and where it is used. We’ll also talk about how you can implement it by showing an example in java.

First, it would be good to know that several different types of factory design patterns exist. For example:

It can be quite confusing with all the different factory types. People referring to the factory pattern are usually talking about the simple factory design pattern.

What is simple factory design pattern?

Simple factory design pattern is one of the most used design patterns along with the singleton pattern we covered in the previous post. A factory is a class that creates objects of the same type (they belong to the same hierarchy). Class itself contains a method that takes in a parameter. By the value of this parameter the factory decides what type of object to instantiate.

In other words, client calls the method in the factory to create an object and tells it the type of the object it is expecting to get back. In a usual case a class would create objects itself through a constructor by using the new keyword.

Simple factory design pattern encapsulates the instantiation of concrete object types. As a result, when new subclasses get added or removed from the class hierarchy, the client code doesn’t have to change.

Where is simple factory design pattern used?

Simple factory design pattern would be used when client/consumer of the created objects doesn’t or shouldn’t know how to create objects of concrete types.

Here are some example usages:

  • Vending machine that sells candies (or other products).
  • Coffee machine that makes various types of coffee: espresso, cappuccino, latte etc.

How to implement simple factory design pattern?

Lets implement a simple factory class CoffeeFactory for a coffee machine that brews different types of coffee: espresso, latte and cappuccino.

First, we need to create an interface Coffee and subclasses Espresso, Latte and Cappuccino. These subclasses will implement the Coffee interface and its’ brew() method. The brew() method is going to be called out by the client to brew coffee of desired type.

public interface Coffee {
    String brew();
}
public class Espresso implements Coffee {

    @Override
    public String brew() {
        return "Brewing espresso";
    }
}
public class Latte implements Coffee {

    @Override
    public String brew() {
        return "Brewing latte";
    }

}
public class Cappuccino implements Coffee {

    @Override
    public String brew() {
        return "Brewing cappuccino";
    }
}

Now, in order for the client to have the possibility to create coffee, we also need to create our simple factory class CoffeeFactory.

public class CoffeeFactory {

    public Coffee create(final String coffeeType) {

        switch (coffeeType) {
            case "ESPRESSO":
                return new Espresso();
            case "LATTE":
                return new Latte();
            case "CAPPUCCINO":
                return new Cappuccino();
            default:
                throw new IllegalArgumentException(
                        "Wrong coffee type: " + coffeeType
                );
        }
    }
}

As you can see, the method create() takes in a parameter coffeeType by what it decides which coffee to create for the client. Note that its’ return type is interface Coffee.

As a final step lets test our simple factory and see what happens when our client tries to brew some coffee.

    // Lets create our factory
    CoffeeFactory coffeeFactory = new CoffeeFactory();

    // Create coffee of type espresso and brew it
    Coffee espresso = coffeeFactory.create("ESPRESSO");
    System.out.println(espresso.brew());

    // Create coffee of type latte and brew it
    Coffee latte = coffeeFactory.create("LATTE");
    System.out.println(latte.brew());

    // Create coffee of type cappuccino and brew it
    Coffee cappuccino = coffeeFactory.create("CAPPUCCINO");
    System.out.println(cappuccino.brew());
}

Here is the output of the code:

Brewing espresso
Brewing latte
Brewing cappuccino

Coffee can be now created and brewed by the client. Even better, the client has no clue of  how it is made. This because we encapsulated all the creation logic into our simple factory class.

Conclusion

Simple factory design pattern is one of the most used design patterns. We would use it when we don’t want the client to know how objects of concrete type get created. Therefore, the client code doesn’t change when new subtypes get added or removed from the hierarchy. Finally, we also created a simple coffee factory as an example.

One thought on “Simple Factory Design Pattern and How to Implement It in Java

  1. In above post Simple Factory Design Pattern And How To Implement It In Java
    In CoffeeFactory {
    public Coffee create(final String coffeeType) {

    Could you please elaborate why coffeeType is final?

Leave a Reply

Your email address will not be published. Required fields are marked *