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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners