Design Patterns — Factory

Elijah Wines
3 min readFeb 21, 2023

--

Intro

In this article, we will be discussing a Creational Design Pattern known as a Factory. We will also go over an example of this pattern in Java. Before we begin, let’s take a look at what a Design Pattern is, and why you should familiarize yourself with them.

Design Patterns

A Design Pattern is not a programming language, or a specific type of technology you can use. It is a paradigm, or, a pre-thought out solution to common problems you will encounter when building projects. Many scenarios you encounter when coding have probably already been encountered before. Instead of trying to re-invent the wheel, why not just utilize a solution that has already proven itself many times over? This is why you should learn Design Patterns on your coding journey.

There are many Design Patterns, and multiple different groups of Design Patterns. There are also many resources available that can aide you in learning these common solutions as well. You can also check out my previous articles here, where I have discussed other Patterns, as well as algorithms and other useful coding topics!

Factory Design Pattern

The Factory Design Pattern works with a superclass that has multiple subclasses, and instantiates one of the subclasses. The Factory's job is to determine which subclass we need to instantiate. Because this pattern focuses on creating new Objects, it is known as a Creational Design Pattern. It even implements one of the Four Pillars of Object Oriented Programming because it hides the internal object creation logic from the Client.

Example

In my example, I will be replicating a gaming system choosing which game the system will start playing. Here is a step-by-step of what we’ll be doing to implement the Factory

  1. Create our Factory Interface called PlayGame. This interface will specify that our subclasses will need to have a startGame method.
  2. Create the subclasses that represent the games our gaming system will be starting. One of these classes will be instantiated depending on the input to the Factory class.
  3. Create our Factory class. This class will have a function called createGameStart that takes a string as an input. It will then decide which game class it will instantiate based on the input.

Now that we have it all planned out, let’s start coding!

Factory Interface

Create an interface that enforces its subclasses to have the startGame method.

public interface PlayGame {
void startGame();
}

Game Subclasses

Create our subclasses that have the startGame method.

public class ZeldaBotW implements PlayGame {

@Override
public void startGame() {
System.out.println("You started playing The Legend of Zelda: Breath of the Wild!");
}

}
public class MarioOdyssey implements PlayGame {

@Override
public void startGame() {
System.out.println("You started playing Super Marion Odyssey!");
}

}
public class PokemonViolet implements PlayGame {

@Override
public void startGame() {
System.out.println("You started playing Pokemon Violet!");
}

}

Factory Class

Create our Factory that will determine which subclass to instantiate.

public class PlayGameFactory {
public PlayGame createGameStart(String game)
{
if (game == null || game.isEmpty()) {
return null;
}
switch (game) {
case "ZeldaBotW":
return new ZeldaBotW();
case "MarioOdyssey":
return new MarioOdyssey();
case "PokemonViolet":
return new PokemonViolet();
default:
throw new IllegalArgumentException("I don't know this game: " + game);
}
}
}

Service/Client Class

Put our Factory to the test!

public class PlayGameService {
public static void main(String[] args) {

PlayGameFactory factory = new PlayGameFactory();
PlayGame play = factory.createGameStart("PokemonViolet");
play.startGame();

}
}

// returns "You started playing Pokemon Violet!"

Now we can see that depending on what is passed into our Factory (createGameStart) method, our factory will instantiate a different subclass! This is the power of the Factory! We have complete control over what happens when we start our games, and we can even add more customization to what happens once the game starts.

Conclusion

In this article we discussed the Creation Design Pattern known as the Factory. We learned that the Factory pattern is useful when needing to instantiate a particular object based on input, which gives us better organization and customization options. Overall the Factory is yet another tool we can add to our belt when creating well structured applications. I hope you all will continue to learn Design Patterns to become stronger Software Engineers. Feel free to check out my page to learn about other coding topics. Happy coding!

--

--

Elijah Wines

Software Engineer - JavaScript, Ruby, React, Ruby on Rails