O'Reilly logo

Learning PHP Design Patterns by William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required