Creational Design Pattern in GO

Rojesh Shrestha
3 min readAug 12, 2019
Photo by Patrick Tomasso on Unsplash

Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a best-suited way based on our requirements. They help in the abstraction of the whole instantiation process. They help make a system independent of how its objects are created, composed, and represented.

Some of the most used creational patterns in GO are:

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

Singleton Design Pattern

The singleton pattern is a software design pattern that restricts the instantiation of a class to one object.

In short, it ensures a class only has one instance, and provide a global point of access to it. Whenever any goroutine tries to access an instance of a variable, you should get the same variable.

Some of the example use-cases for using this design pattern are:

  1. When we want to use the same connection to a database to make every query
  2. When you open an SSH connection to a server to do a few tasks
  3. If you need to limit the access to some variable or space.
  4. If you need to limit the number of calls to some places.

A simple implementation of this pattern in GO is given below:

Builder Design Pattern

This design pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. It avoids writing the logic to create all the objects in the package.

This design pattern creates an object step by step by filling its fields and creating embedded objects. It reuses the object creation algorithm between many objects.

Let us consider an example of its implementation in a vehicle manufacturing process. We could use the same technique to build a car that has been used to create a bus except they will be of different size and seat.

Implementation

Usage

md := ManufacturingDirector{}

carbuilder := &CarBuilder{}
md.SetBuilder(carbuilder)
md.Construct()
fmt.Printf("%+v",carbuilder.GetVehicle())
bikebuilder := &MotorbikeBuilder{}
md.SetBuilder(bikebuilder)
md.Construct()
fmt.Printf("%+v",bikebuilder.GetVehicle())

Factory Method Design Pattern

This design pattern allows creating an object without exposing the creation logic to the client and refers to the newly-created object using a common interface. It provides a simple way of extending the family of objects with minor changes in application code and promotes the loose-coupling by eliminating the need to bind application-specific structs into the code.

Implementation

Usage

db1 := factory.DatabaseFactory("production")
db1.putData("test", "this is mongodb")
fmt.Println(db1.getData("test"))
db2 := factory.DatabaseFactory("development")
db2.putData("test", "this is sqlite")
fmt.Println(db2.getData("test"))

Prototype Design Pattern

It specifies the kind of objects to create using a prototypical instance, and create new objects by copying this prototype. The aim of this pattern is to have an object or a set of objects that are already created at compilation time, but which you can clone as many times as you want at runtime. It helps in avoiding repetitive object creation.

Implementation

Usage

u := prototype.NewUser("email@gmail.com", "9876543")
uu := u.WithEmail("email@hotmail.com").WithPhone("999999") fmt.Println("Original User Object ", u)
fmt.Println("User object copied during runtime ", uu)

These are some of the commonly used creational design patterns in Go in order to create objects with ease and efficiency based on our use cases and requirements.

--

--