From the course: Design Patterns: Creational

How the pattern works

From the course: Design Patterns: Creational

Start my 1-month free trial

How the pattern works

- [Instructor] To see how we might use the Factory Method pattern, consider an example where we have two different kinds of pizza factories. One for New York style pizza, which makes a thin crust pizza, and one for Chicago style pizza, which makes a thick crust pizza. The Order Pizza Method in the Pizza Factory Super Class, is the same for both factories, but the Create Pizza Method is implemented by the two concrete factories. If you call Create Pizza on the New York factory, the factory will make New York style pizza products. And likewise, if you call Create Pizza on the Chicago factory, the factory will make Chicago style pizza products. Both the concrete pizza types implement a common pizza interface. So as with Simple Factory, the client never has to worry about the concrete type of pizza. The client deals only with the pizza interface. As with Simple Factory, the benefit here is that we can come along and add new types of factories that make different pizza products without having to modify the code in the rest of the system. All the patterns in the gang of four catalog have a category and a definition, or intent. The category of Factory Method is creational, and the inheritance relationship between the factory interface and the concrete factories is a key part of the pattern. Which product we get is determined by which concrete factory we choose to make them. As a creational pattern, Factory Method is primarily concerned with how objects are instantiated, and it decouples the clients from the objects it needs to instantiate. The pattern's definition describes the pattern in a couple of short sentences. The Factory Method pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to the subclasses. Taking another look at the class diagram for the Factory Method pizza example, we can see how that definition describes the pattern. The pizza factory class defines an interface for the factories with its order pizza and create pizza methods, but defers instantiation of pizza objects to the concrete pizza factories by making the create pizza method abstract. The concrete factories define this method and instantiate the pizza product that matches the factory that's doing the instantiation. The Factory Method pattern embodies several design principles, including encapsulate what varies, program to the interface not the implementation, the open closed principle, and depend on abstractions. With Factory Method we're encapsulating the different kinds of pizzas that need to get made as well as the code to create them in the factories. We're programming to the pizza interface in our client code so we can change the pizza concrete types without having to change the client code. Our factory code is open to new types of pizzas but the client is closed for modification. This means we can extend the system with fewer opportunities for unexpected bugs to arise in the process. And we're depending on abstractions. We've taken all the code that refers to concrete pizzas completely out of the client, and encapsulated it into a factory class and made sure the client refers only to the pizza interface, not the concrete types of pizzas. Now the concrete pizza types, as well as the client, depend on the pizza abstraction, while the factory is the only part of the system that depends on the concrete pizza class types.

Contents