Factory Pattern

In this lesson, you will learn about the factory pattern in detail with the help of a coding example.

What is the factory pattern?

The factory pattern is a creational pattern that provides a template that can be used to create objects. It is used in complex situations where the type of the object required varies and needs to be specified in each case.

It does not use the new keyword directly to instantiate objects. This means it does not explicitly require the use of a constructor to create objects. Instead, it provides a generic interface that delegates the object creation responsibility to the corresponding subclass.

Example

As the name “factory” implies, we can use this pattern when we want to create different objects that have some similar characteristics. Let’s look at an example to understand this better.

Consider the example from the previous chapter where an ice cream factory can be used to create multiple flavors of ice cream. Let’s see how we can apply the factory pattern in this example.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy