Design Patterns — Abstract Factory Pattern && Implementation in PHP

Backend Developer
2 min readFeb 20, 2022


Abstract Factory is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes.

Abstract Factory

This design pattern is used for creating an interface for all related /dependent objects instead of concrete classes. Sounds good! Wait.. I did not get anything from this?

Abstract Factory defines an interface to create all the different products, but leaves the actual product creation to concrete factory classes.Each factory type corresponds to a specific product variety.

Before everything we should create a back bone of our structure which is an abstract class to define main component. We do not define any action in this abstract class/interface. We just create our main methods which return different abstract products.

The Abstract Factory interface declares creation methods for each distinct product type.

Each Concrete Factory corresponds to a specific variant (or family) of products.

As you can see we created 2 concrete classes which implements our main interface. Blade and Smarty Template Factories , you can extend with more engines… I know you just noticed that we have method returns such as TitleTemplate,PageTemplate, Render. We should create new interfaces for each distinct product.

Lets create some classes which are implement those interfaces…

Last part is about client side. All above is abstraction of factory. And now we will implement the abstraction on to real code base.

As you can see above, you can pass either BladeTemplateFactory or SmartyTemplateFactory it will work depends on which factory is selected. So you can change your template engine without touching your main codebase. Abstract Factory is generally used for this purpose…

In short we create a back bone interface independent from all sub types of products and then we implement that on a base class with new types of product. So we will add new interfaces to define types we declare on that abstract class. Why we use interfaces? Just because we will implement them to derive same type classes.

I use more codes than explanations just because code can explain it more effectively, you can as questions via using responses section below. Hope i can explain it.

If you like my articles clap and follow me for more contents from me. I will go on with other design patterns on creational side.

Thanks for reading…



Backend Developer

Senior Php Developer | Javascript | Python | Ruby