Creational Design Patterns with Code Examples in Java

Hanwen Zhang
4 min readJul 23, 2023

--

Key Takeaways of Creational Design Patterns for Java Design Patterns, 2nd Edition by Vaskaran Sarcar

Summarize what I have learned about Abstract Factory, Factory Method, Abstract Factory, and Build Design Patterns in Object Oriented Programming with code examples (Java).

For an overview of all 23 GOF Design Patterns, Read more

Talk is cheap, show me the code.

Photo by Aleks Dorohovich on Unsplash

Abstract Factory v.s. Factory Method

Note that an abstract factory design pattern creates the factory which deals with the creation, no subdivision; while the factory method design pattern creates the product, we pass our rules to decide the subclass to instantiate.

Abstract Factory

The Abstract Factory pattern provides an interface as a root of the family for creating related dependent objects without specifying concrete classes.

//Abstract Factory
interface AnimalFactory{
Dog createDog();
Tiger createTiger();
}
//Concrete Factory-Wild Animal Factory
class WildAnimalFactory implements AnimalFactory{
@Override
public Dog createDog() {
return new WildDog();
}
@Override
public Tiger createTiger() {
return new WildTiger();
}
}
//Concrete Factory-Pet Animal Factory
class PetAnimalFactory implements AnimalFactory{
@Override
public Dog createDog() {
return new PetDog();
}
@Override
public Tiger createTiger() {
return new PetTiger();
}
}

Simple Factory

In the Simple Factory design pattern, you have an if-else case to decide which object to create and return. The downside of this is if a new type of object has to be re-created, we need to modify the if-else cases.

public class SimplePizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;

if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("pepperoni")) {
pizza = new PepperoniPizza();
} else if (type.equals("clam")) {
pizza = new ClamPizza();
} else if (type.equals("veggie")) {
pizza = new VeggiePizza();
}
return pizza;
}
}

Factory Method

The Factory Method pattern delegates to subclasses to decide which object to create and return. The downside of this is you need a separate class for each of the cases. The advantage is existing code will not be touched.

abstract class AnimalFactory {
/*Remember that the GoF definition says "....Factory method lets a class defer instantiation to subclasses."
In our case, the following method will create a Tiger or Dog but at this point it does not know whether
it will get a Dog or a Tiger. This decision will be taken by the subclasses i.e.DogFactory or TigerFactory.
So, in this implementation, the following method is playing the role of a factory (of creation)*/

public abstract Animal createAnimal();
}

class DogFactory extends AnimalFactory {
public Animal createAnimal(){
//Creating a Dog
return new Dog();
}
}

class TigerFactory extends AnimalFactory {
public Animal createAnimal(){
//Creating a Tiger
return new Tiger();
}
}

Builder

The Builder design pattern constructs a complex object step by step, and with the same construction process, it can create different representations. There are 4 parts in the Builder pattern:

  • Creator Class: handles the main method where you instantiate the director class and its construct method is being called.
  • Director Class: generates the final product object where you instantiate the builder class and its setters and getters with values being passed.
  • Builder Class: defines all of the steps that need to be taken in order to create a product, here you instantiate the base class and give it meaningful functionalities to create a particular product.
  • Base Class: the type of complex object that is to be generated by the builder pattern with fundamental attributes and methods.

--

--

Hanwen Zhang

Full-Stack Software Engineer at a Healthcare Tech Company | Document My Coding Journey | Improve My Knowledge | Share Coding Concepts in a Simple Way