Builder design pattern in java

Builder design pattern allows to create complex object step by step and also enforces a process to create an object as a finished product.Construction of object should be such that same construction process can create different representations.Director controls the construction of object and only director knows what type of object to create.

For example, You can consider printing of a book.Printing of a book involves various steps such as printing table of content,preface,introduction,chapters and conclusion.Finally you will get complete book object. With the help of same above process,you can print books with different properties.

As described by Gof:
“Separate the construction of a complex object from its representation so that the same construction process can create different representations”

Generic UML diagram for builder design pattern:

Elements:

  • Builder
    • specifies an abstract interface for creating parts of a product object.
  • ConcreteBuilder
    • constructs and assembles parts of the product by implementing the builder interface.
    • defines and keeps track of the representation it creates.
    • provides an interface for retrieving the product.
  • Director
    • constructs an object using builder interface.
  • Product
    • represents the complex object under construction.ConcreteBuilder builds the product’s internal representation and defines the process by which it is assembled.
    • includes classes that define the constituent parts,including interfaces for the assembling the parts into final result.

When to use it:

  • Object creation algorithms should be independent of system.
  • New creation algorithm can be added without changing core code.
  • The construction process must allow different representations for the object that’ s contructed.
  • Runtime control over creation process is required.

WorkFlow:

  • The client creates the director object and configures it with the desired builder object.
  • Director notifies builder whenever a part of product should be built.
  • Builder handles requests from the director and adds parts to the product.
  • The clients retrieves the product from builder.

Example:

You can consider priniting of a book.Printing of a book involves various steps such as printing table of content,preface,introduction,chapters and conclusion.Finally you will get complete book object. With the help of same above process,you can write books with different properties.  BookWriter will instruct bookBuilder to print book in steps and return final book object.

Comparing to generic UML diagram of builder pattern:
  • BookBuilder(Builder)
  • TechnicalBookBuilder(ConcreteBuilder)
  • FictionalBookBuilder(ConcreteBuilder)
  • BookWriter(Director)
  • Book(Product)

Java codes for above classes:
Following class is our product class.Object of this class will be returned by builder.

Book.java (Product):

Following interface is our builder interface.Builder interface provides steps or process.Here we have five steps-buidTableOfContent,buildPreface,buildIntroduction,buildChapters,buildGlossary.It also has method to return book(product) object. 
BookBuilder.java (Builder):

Following class is our first implementation of builder interface.We are having multiple concrete builder class to support  same construction process creating multiple representations.

TechnicalBookBuilder.java(ConcreteBuilder):

Following class is our second implementation of builder interface.

FictionalBookBuilder.java(ConcreteBuilder):

Following class is our director class which will instructs BookBuilder to print parts of book and return final book object
BookWriter.java(Director):

BuilderDesignPatternMain.java:

For printing technical book,we have configured bookWriter object with technicalBookBuilder.BookWriter instructed to technicalbookbuilder to print book and return final book object.Same is true for fictional book.So with help of same construction process,we have printed two kinds of book.i.e. technical and fictional.
Output:

References:The Gang of Four (GoF) book

Related Posts

  • Detect keypress in Python
    12 June

    Detect keypress in Python

    Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

  • Copy a dictionary in Python
    12 June

    7 ways to copy a dictionary in python

    Table of ContentsUsing the copy() function to copy a dictionary in PythonUsing the deepcopy() function to copy a dictionary in PythonUsing the dict() function to copy a dictionary in PythonUsing the unpack operator ** to copy a dictionary in PythonUsing the update() function to copy a dictionary in PythonUsing the dictionary comprehension method to copy […]

  • Escape quotes in Python
    11 June

    Escape quotes in Python

    Table of ContentsUsing \ to escape quotes in PythonUsing \ with replace() to escape quotes in PythonUsing json.dumps to escape quotes in PythonUsing triple single or double quotes to escape quotes in pythonUse r to include \ and quotes in String 💡 Outline You can use \ to escape quotes in Python. If you want […]

  • Merge two vectors in C++
    11 June

    Merge two Vectors in C++

    Table of ContentsUsing merge() to merge two vectors in C++Using set_union() to merge two vectors in C++Using insert() to combine two vectors in C++Using move() & back_inserter to merge two vectors in C++Using copy() and back_inserter to concatenate two vectors in C++Using make_move_iterator() and insert() to merge two vectors in C++Conclusion vectors are like dynamic […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.