Related Articles

A Complete Guide To Design Patterns: Creational Patterns

Will be going through the Creational Design Pattern illustrating how and where to use it.

Baraa Abuzaid
A Complete Guide To Design Patterns:...

Over the course of your career as software developer, you will encounter problems that are very common, developers over the years have addressed and find an optimum solutions for them to never go through the hassle of building your own solution. The design patterns are very powerful tools and its the industry’s preferred method of tackling many recurring problems.

Why use design patterns?
The biggest advantage is that they have already been proven by experts.
In addition, it acts like an expressive vocabulary when trying to discuss a design with another developer.

What are the types of design patterns?
Design patterns be could be categorized into three different categories.

  • Creational patterns.
  • Structural patterns.
  • Behavioural patterns.

However, this article will discuss the creational design pattern. In particular the Factory Design Pattern. Creational patterns are simply patterns that concern with object creation.

A) Factory Design Pattern

Factory Design pattern is a creational design pattern. nevertheless, when using Factory Design pattern we delegate objects creations. however, the delegation of object creation could be done by using The Factory Objects Pattern or The Factory Method Pattern.

The Factory Objects Pattern

Imagine a scenario where you have to create a software for a dealership that sells cars online. meanwhile, you have to be able to create different types of cars and add them to the inventory furthermore, there are certain operations you are required to do before adding a car to the inventory like clear it from customs, create insurance documents and create service documents. So here is how we could address that by using factory objects.

/** This is the factory object, with role of create new car object  **/
public class CarFactory {

    public Car create(String type){
        if (type.equals("convertible")){
            // ConvertibleCar is a subclass of a Car
            return new ConvertibleCar();
        }else if(type.equals("sport")){
            // SportCar is a subclass of a Car
            return new SportCar();
        }else if (type.equals("suv")){
            // SuvCar is a subclass of a Car
            return new SuvCar();
        return null;

/**Dealership class make use of a CarFactory to create a new car object **/
public class Dealership {
    CarFactory carFactory;

    public Dealership(CarFactory carFactory) {
        this.carFactory = carFactory;

    public Car addToInventory(String type){
     Car car = carFactory.create(type);

     // prepare the car

     return car;


The Factory Method Pattern

In the Method Factory pattern, we create an abstract class called the creator in our case is Dealership class the creator abstract class provides a blueprint for the car creation method and let the subclass define the exact implementation of the factory method. Moreover, we could have several concrete creators classes with each having its own implementation of the factory method. So we could have ElectricCarDealership , GasolineCarDealership, and a DieselCarDealership and everyone have its own implementation of createCar() method.


public abstract class Dealership {

    protected void prepareCar(String carType){

        Car car = createCar(carType);
    protected abstract Car createCar(String type);


public class ElectricCarDealership extends Dealership {
    protected Car createCar(String type) {
        if (type.equals("electricSportCar")){
            return new ElectricSportCar();
        }else if (type.equals("electricSuvCar")){
            return new ElectricSuvCar();
        // .. and so on
        return null;


B) Singleton Design Pattern

It is one of the simplest patterns to implement. The idea comes from the need to have only one instance of the class(object) throughout our application. to achieve that we make the constructor as private. preventing any instantiation of an object by the conventional new keyword. Meanwhile, creating a public static method for creating a new instance of an object. Then inside the method, we check for the nullability to decide whether we create a new instance or returning an old instance if its already been created.


public class PrinterDriver {
    public static PrinterDriver instance;

    private PrinterDriver() {

    public static PrinterDriver getInstance(){
        if (instance == null)
            return new PrinterDriver();
            return instance;


Feature Photo by Quino Al 

Show Comments (0)


Related Articles

Object Oriented Design

A Complete Guide To Design Patterns: The Adapter Design Pattern

The adapter design pattern is a structural design pattern. As the name drove from the adapter in the physical world, the adapter design pattern work in a similar way. In many...

Posted on by Baraa Abuzaid

A Complete Guide To Design Patterns In Kotlin: Composite Design Pattern

A composite design pattern is built upon two principles in object-oriented programming. First, decomposition by breaking a whole into parts. Then generalization which generalizing...

Posted on by Baraa Abuzaid