Abstract Factory Design Pattern with real life example in PHP

Hasan Sheikh
Towards Dev
Published in
2 min readOct 26, 2022

--

Abstract Factory Desing Pattern

If you don’t know about Factory Pattern. Please check out my previous article. I already write about it Factory design pattern with real-life example (PHP)

What is an abstract factory design pattern?

Abstract Factory design pattern is one of the Creational pattern. Abstract Factory pattern is almost similar to Factory Pattern is considered as another layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories.

Simply Say

Abstract factory purpose it to create an abstraction for creation of family of products, so you can easily change it to different family.

I know it’s difficult to understand in theory. It’s better to create a scenario.

Story

If you go outside your country or city. Maybe you booked a hotel. This hotel has package types like standard & premium type. You can select any package right? Let’s build our booking system using an abstract factory design pattern.

First, we create Package an abstract class. Package will extends all package.

Package.php

Now it’s time to create a package e.g Standard, Premium. This class will extend the Package abstract class.

Standard.php
Premium.php

Our package creates done. Now we create a factory for packages. CreateBookingFactory class has one create method. This method create only a package.

CreateBookingFactory.php

Now we create factories for all packages.

StandardFactory.php
PremiumFactory.php

At the end position now we need to create the factory of a factory. Maybe notice BookingFactory returning a Factory

BookingFactory.php
Output.php
Result

🤗 I hope everyone enjoys it.
🙋 Follow & Subscribe: https://proghasan.medium.com/subscribe

Design Pattern

  1. Factory design pattern with real-life example (PHP)
  2. Abstract Factory Design Pattern with real life example in PHP

--

--