FuzzDuck
FuzzDuck
Factory pattern
FuzzDuck
Factory pattern

Factory pattern

The main classes of the Factory pattern is the Factory and the Product. With the factory pattern, a Client is able to create new classes through a factory class. The difference between using the new() method to create a new class is that the factory pattern allows you to customize what is going to happen when creating a new instance of the product class.

Factory Pattern UML

The Factory

Like the factory pattern directly suggest, it can be compared to a factory. From the factory you can order a variety of products. All you do is call the Factory, and ask them to create a specific product and have it sent to your door. For example the factory could be making different kinds of furniture. With the factory pattern, you can call the Factory class and tell which kind of product you wish for, by specifying the parameters of the ConcreteProduct class in the createProduct() method.

The Ice Cream Machine

In this example we have an “Ice Cream Machine” running making all different types of ice cream. When we want a new ice cream, we call a method to the machine to create another instance. The machine takes care of creating the ice creams through the method “createIceCream()”. Through the “createIceCream()” method, it is possible to customize, which kind of ice cream you would like.