Golang Builder Pattern (Creational)

After a basic explanation of Abstract Pattern in Golang. There is need that, I should explain a more powerful design pattern, which will help in creating more complex objects. The pattern is Builder Pattern.

What is Builder Design Pattern

Design pattern books are boring. You may disagree but it’s my personal feeling. hehehe… But I will try make this part a bit interesting. Every book has example of same food company, even websites explaining design pattern are using same examples again and again.

But I will first user some theoretical terms and then with help of diagrams, I will explain it. Builder pattern comes in to picture when you have complex product. Means a product with may inputs, for e.g. A Car company like Audi, manufactures many kind of cars, although they all are cars but all are different, Audi Q3 , Audo Q4, Audi R8. How they accomplish it in a same factory is an example of Builder Pattern.

In builder pattern we just need to remember five components:

Director: Director performs all the required steps given inside builder to create a product. In our case our production line in our director. It will invoke all the abstract builder steps to create our Audi.

Abstract Builder: Abstract builder provides common interface for concrete builder. All the method inside our abstract builder will be implemented by our concrete Audi builders.

Concrete Builder: There will be multiple concrete builders, one for each kind of Audi.

Product: Audi is our product and based on builder we will decide which Audi it is.

When all the above components work together, it makes a builder pattern. I know it’s not easy to grasp but believe me GO code I will write, will explain you everything.


The above drawing, just explains, Director consists of an abstract builder and based on input, director will invoke a concrete builder to create out favorite Audi.

Golang Code For Builder Pattern

package main

import "fmt"

//this is Product.
type MyAudi struct {
       name string
       wheelSize string
       engineCapacityCC int
       powerBHP int
       torque int
       fuelType string
       cost int

func (m MyAudi) showAudi() {
       fmt.Println("Audi Name : " , m.name)
       fmt.Println("Audi wheel : " , m.wheelSize)
       fmt.Println("Audi Engine : " , m.engineCapacityCC)
       fmt.Println("Audi power bhp : " , m.powerBHP)
       fmt.Println("Audi Torque : " , m.torque)
       fmt.Println("Audi fuel : " , m.fuelType)
       fmt.Println("Audi Cost INR: " , m.cost)


//this is abstract builder
type AudiBuilder interface {
       createAudi() MyAudi

//now concrete builders
type AudiQ3Builder struct {

func (a AudiQ3Builder) createAudi() MyAudi {
       audi := MyAudi{}
       audi.name = "AydiQ3"
       audi.cost = 1000000000
       audi.engineCapacityCC = 3000
       audi.fuelType = "Diesel"
       audi.powerBHP = 300
       audi.torque = 480
       audi.wheelSize = "R17/75/250"
       return audi

//now concrete builders
type AudiQ4Builder struct {

func (a AudiQ4Builder) createAudi() MyAudi {
       audi := MyAudi{}
       audi.name = "AydiQ4"
       audi.cost = 2000000000
       audi.engineCapacityCC = 4000
       audi.fuelType = "Patrol"
       audi.powerBHP = 400
       audi.torque = 580
       audi.wheelSize = "R19/85/250"
       return audi

// you may implement other builder urself :P

//this is director

type ProductionLine struct {
       builder AudiBuilder

func (a ProductionLine)CreateMyAudi() MyAudi {
       return a.builder.createAudi()

func main() {
       p := ProductionLine{AudiQ3Builder{}}
       res := p.CreateMyAudi()
       p = ProductionLine{AudiQ4Builder{}}
       res = p.CreateMyAudi()



Audi Name : AydiQ3
Audi wheel : R17/75/250
Audi Engine : 3000
Audi power bhp : 300
Audi Torque : 480
Audi fuel : Diesel
Audi Cost INR: 1000000000
Audi Name : AydiQ4
Audi wheel : R19/85/250
Audi Engine : 4000
Audi power bhp : 400
Audi Torque : 580
Audi fuel : Patrol
Audi Cost INR: 2000000000

Dear friends, I tried to keep the example easy, but this pattern solves way more complex problems then this. Hope this may help you to understand this pattern.

Next in line is a RSA encryption based file transfer program, which will help you to understand, how PKI works basically.

Happy coding. 🙂



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s