Factory design pattern in Java with Example

The Factory pattern is a parameter driven creational design pattern. It is one of the most popular design patterns in Java. The Factory pattern does not expose the object instantiation logic to the client by deferring to the sub classes. All the client knows about is common interface that the factory exposes.

Lets implement an example of the Factory pattern for a simple workout application that lets you create a specific workout. We are going to create a Workout interface that contains a createWorkout() method. We will then create two concrete sub classes that inherit Workout, Upperbody and Lowerbody. We will then create a WorkoutFactory class that will be called from the client main method. WorkoutFactory will instantiate an object for us depending on the parameter that was passed into getWorkout(workoutType).

1. Create the Workout interface.

public interface Workout {
    void createWorkout();
}

2. Create the UpperBody and LowerBody sub classes that inherit Workout.

public class LowerBody implements Workout {

    @Override
    public void createWorkout() {
        System.out.println("Created Lower Body Workout that includes:");
        System.out.println("1. Squat");
        System.out.println("2. Lunges");
        System.out.println("3. Calf raises");
    }
}
public class UpperBody implements Workout {

    @Override
    public void createWorkout() {
        System.out.println("Created Upper Body Workout that includes:");
        System.out.println("1. Bench Press");
        System.out.println("2. Push ups");
        System.out.println("3. Incline Bench Press");
    }
}

3. Create the WorkoutType enum to avoid using hard coded strings.

public enum WorkoutType {

    UPPERBODY, LOWERBODY;
}

4. Create a WorkoutFactory class that will contain a static method getWorkout(workoutType). This will be static so we don’t have to create an instance of WorkoutFactory. Instead, we just reference from the class name in the client. This method will create a object for us based on the parameter that is passed.

public class WorkoutFactory {

    public static Workout getWorkout(WorkoutType workoutType) {
        switch (workoutType) {
            case UPPERBODY: {
                return new UpperBody();
            }
            case LOWERBODY: {
                return new LowerBody();
            }

            default: {
                return null;
            }
        }
    }
}

5. In your client/main method, use the WorkoutFactory class to get the workout based on the WorkoutType. The client only cares about the Workout object and doesn’t directly interface with any sub classes here.

public class Main {

    public static void main(String[] args) {
        //Retreives the UpperBody object
        Workout workout = WorkoutFactory.getWorkout(WorkoutType.UPPERBODY);
        
        //Calls createWorkout from UpperBody
        workout.createWorkout();

        //Retrieves the LowerBody object
        workout = WorkoutFactory.getWorkout(WorkoutType.LOWERBODY);

        //Calls createWorkout from LowerBody
        workout.createWorkout();
    }
}


6. Verify the output.

Created Upper Body Workout that includes:
1. Bench Press
2. Push ups
3. Incline Bench Press
Created Lower Body Workout that includes:
1. Squat
2. Lunges
3. Calf raises

 That’s the basic idea of the Factory pattern. This is a simple example but this pattern can get quite complex in a enterprise size application. This pattern is almost the opposite of the Singleton pattern. Singleton returns the same instance and has no interface or subclasses whereas the Factory pattern returns multiple instances and contains sub classes and interfaces.