Factory Design Pattern

Factory Method is a creation design pattern that provides an interface for creating objects and allows subclasses to alter the type of objects that will be created.

Imagine that you’re implementing a mailing feature . At first you are sending email with mailgun and all of your business source code are all in one place.

Later you have to implement using another service called mailchimp. So you have to overwrite you source code again. If you are going to use both of the services, your code will be messy to be able to work for both services.

At this point , we can use factory design pattern, by creating objects for both service classes. So, we gonna build a mailing interface with an abstract function sendmail first. Then we will have two sub classes to implement that interface (mailgun and mailchimp classes for now). Inside these classes, there will be a function to override the abstract function from interface called sendmail which will be different in implementation according to classes.

At the final step, we will create factory classes and return mail implementation concrete classes. The client just has to choose which factory to be used, doesn’t has to know the detail implementation of the concrete classes which is the main theme of this factory pattern.

So I will create code samples for the above scenarios. I will write in PHP & C#.

PHP Sample Code

C# sample code


  • loosely coupling as classes are not depending on each other.
  • easy to maintain (extend or remove) concrete classes.


  • Code might be nasty when many concrete classes are implementing to one interface.

By Yuuma




tel. 06-6454-8833(平日 10:00~17:00)