4/180: Design Patterns in JAVA

Today is day 4 of 180 day streak and this is challenge I have taken to develop habit of learning. So today I am going to talk about Main 3 types of Designs Patterns and then I will be talking about builder pattern which is one of this sub types of these 3 Main types of design patterns. Let’s begin

Three types of Design Patterns

  1. Creational Design Patterns
  2. Structural Design Patterns
  3. Behavioral Design Patterns

Creational Design Patterns

It deals with creation/ construction of objects.

Builder / Factories / Abstract Factory / Prototype / Singleton are different types of creational design patterns and I am going to discuss all in the coming days.

Structural Design Patterns

Concerned with structure example class members. Many patterns are wrappers that mimic the underlying class interface. They describe the structure of app.

Adapter / Bridge / Composite / Decorator / Facade /Flyweight / Proxy are different types of structural design patterns

Behavioral Design Patterns

Behavioral design patterns deal with communication between objects and classes.

Chain Of Responsibility / Command / Interpreter / Iterator / Mediator / Memento / Null Object / Observer / State / Strategy / Template Method / Visitor are different types of behavioral design patterns.

Today I am going to discuss about Builder Design pattern which is a type of creational pattern and helps and eases the creation of objects. Before going ahead first of all I am going to give example of very basic builder which we all know about, but were not aware that it is made using builder pattern. It is the String Builder in Java.

What was the need of builder pattern?

When there are multiple parameters and it become difficult to add all those in the constructor we go for the builder pattern. Instead of adding 100’s of parameters which may or may not be required everywhere, we create it using builder pattern.

Having a constructor with 10 arguments is not productive, instead opt for piecewise construction. When piecewise construction of object is complicated, provide an API for doing it succinctly.

Fluent builder

It allows you to use long chains which makes build of objects easy. If you see in StringBuilder sb = new StringBuilder();

to append something we do sb.append(“hello”); this return type of it is StringBuilder itself, which allow us to chain it further. sb.append(“hello”).append(“ World!”); forming and chain and removing extra line of code we do it all in same line.

Faceted Builder

Till now we saw the use of single builder to build objects, but it is not always the case. Some times the object you are building is so complicated that single builder is not sufficient and you need more than one builder. When you need to create multiple builders and access it through one main builder, it uses facade design pattern also.

BuilderFacated.java (github.com)

If you see in above code we have different builders of person for address and employment.

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.