Source: triumph-job.medium.com

Factory Method Pattern

Category: PHP, PHP

Earlier we talked about the Singleton and Strategy design patterns, about the subtleties of implementation in the Golang language.

The Factory method, also known as the Virtual Constructor, is an astounding design pattern that defines a common interface for creating objects in a parent class and allows you to modify the created objects in child classes. The pattern allows a class to delegate object creation to subclasses.

You find that most of your entities in the program are strongly related to the Scooter object, and in order to make your program work with other delivery methods, you will have to add links to 80% of your codebase and repeat this for each new transport.

But now you can override the factory method in your subclass to change the type of transport you create.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners