Chapter 5. Factory Method Design Pattern

Women’s movements would form among the factory workers, a great mobilisation that destroyed the old models.

Emma Bonino

Design is the method of putting form and content together. Design, just as art, has multiple definitions; there is no single definition. Design can be art. Design can be aesthetics. Design is so simple, that’s why it is so complicated.

Paul Rand

Create your own method. Don’t depend slavishly on mine. Make up something that will work for you! But keep breaking traditions, I beg you.

Constantin Stanislavski

What Is the Factory Method Pattern?

As part of the creational category of design patterns, the Factory Method pattern is involved with creating something. In the case of the Factory Method, the something created is a product that is not tied to the class that creates it. Instead, in order to maintain loose coupling, the client makes the request through a factory. The factory then makes the requested product. Another way of thinking about it is that the Factory Method frees the product from the requestor. Figure 5-1 shows a class diagram of the Factory Method.

Implemented Factory Method class diagram
Figure 5-1. Implemented Factory Method class diagram

The Client class is implied. As you can see in Figure 5-1, the Client holds a reference to the Creator (factory interface) for requesting a graphic or text product. However, it does not instantiate the product it requests. ...

Get Learning PHP Design Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.