Factory Method
The Factory Method differs from the ordinary Simple Factory on the basis that instead of us having one factory, we can have many.
So why would you want to do this? Well, in order to understand this, we must look to the open/closed principle (OCP). Bertrand Meyer is usually associated with having originated the term open/closed principle in his book, Object-oriented Software Construction. Meyer stated the following:
"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"
Where a software entity needs to be extended, this should be possible without modifying its source code. Those of you who are familiar with the SOLID (single responsibility, open-closed, Liskov substitution ...
Get Mastering PHP Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.