Creational Software Design Patterns

The Software Design Patterns – Creational Patterns (Part 1)

This is Part 1 of the Series: Software Design Patterns. In this tutorial series, I would explain to you the important software design patterns you need to know. There are actually dozens of Software Design Patterns out there. However, many of them are just features natively supported by certain programming languages.

We would examine three categories of Software Design Patterns namely Creational Patterns, Structural Patterns and Behavioural Patterns.

In this part, we would focus on Creational Patterns and we would cover the following:

  1. Factory Pattern
  2. Singleton Pattern
  3. Builder Pattern
  4. Prototype Pattern

We would also try to give example with code snippet on how each is applied.

 

1. Factory Method Pattern

To understand the Factory Pattern, we first need to understand the concept of Factory in programming. A factory is simply an object for creating other objects. It could be a function that returns an object. Such function if called a factory function and it normally invokes the “new”.

For example, in a Spring application,  we can have a factory function is given below that returns a RestTemplate object. This is shown below:

@Bean
public RestTemplate getRestTemplate() {
   return new RestTemplate();
}

 

From the example above, if you need to want a new RestTemplate, you simply declare a private method of RestTemplate and annotate it with @Autowired annotation. Like so:

@Autowired
private RestTemplate restTemplate;

 

Benefits of Factory Pattern

  • subclasses of objects and redefine which classes to instantiate
  • instantiation is deferred to subclases

 

2. Singleton Pattern

This pattern restricts the instantiation of a class to just a single instance. It ensures that

  • only one instance of an object exists
  • provides access to that instance
  • controls creation of new objects

Singleton pattern has the benefit of keeping the global namespace clean and also permits lazy initialization. This is preferred to global variables. To implement singleton class, you will have to:

  • declare all the constructors of the class as private. In this way, the class can’t be instantiated
  • provide a static method that returns a reference to that instance

Example of a singleton class is given below:

public class Student {
    private static Student instance = new Student(); // eagerly loads the singleton
    private int count;
    private Student(){
        //private to prevent instantiation from outside
    }
    
    public static Student getInstance(){
        return instance;
    }
    public int getCount() {
        return count;
    }
}

 

3. Builder Pattern

This is a pattern that breaks down the construction of complex object into several steps. The objective is to separate complex object creation from its representation. The Builder patter follows the approach:

  • encapsulate the creation and assembling of various parts of a complex object into a separate Builder object
  • object creation is delegated to a Builder object instead of creating the object directly

Since demonstrating this pattern is a bit more involved, you can find details in The Builder Pattern

 

4. Prototype Pattern

In the Prototype Pattern, new objects are created based on an existing prototype.

To implement this pattern,  you need to first create a base abstract class. This class would specify a pure virtual clone() function. So classes can then derive from this abstract class implementing the clone() method.

// Prototype Base class
public abstract class Shape implements Cloneable {
    public Shape clone() throws CloneNotSupportedException{
        return (Shape) super.clone();
    }
}

// Derived class Rectangle using the Prototype
public class Rectangle extends Shape {
    @Override
    public Shape clone() throws CloneNotSupportedException {
        return (Rectangle)super.clone();
    }
}

// Derived class Square using the Prototype
public class Square extends Shape {
    @Override
    public Shape clone() throws CloneNotSupportedException {
        return (Square)super.clone();
    }
}

 

kindsonthegenius

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

Your email address will not be published. Required fields are marked *