This tutorial explains Builder design pattern in java with UML class diagram. It then takes an example scenario in java and explains it with class diagram and code.
Introduction: Builder Design Pattern is a creational design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. Being a creational design pattern, the Builder pattern deals with the design of how the object creation is managed.
What is Builder Design Pattern
In a Builder Design Pattern implementation the construction of a complex object is separated from its representation. This enables us to create multiple representations using the same construction process.
In Builder Pattern the class which invokes the the multiple steps to create sub-parts of the final product is loosely coupled to the actual implementation of those steps using an abstract Builder interface in between.
Scenarios in which Builder Design Pattern can be used
Given below are the two conditions which together must be applicable to justify the use of Builder pattern while creating a product –
- Algorithm for creating the product needs to be independent from the sub-parts’ creation & assembly: Builder pattern is useful when the algorithm/steps for creation of a final complex product needs to vary independent of the sub-components for that product which are created and then assembled to get the desired product.
- Different representations of the product are possible: The product being created has variants which are different representations of the same basic product.
Class Diagram for Builder Design Pattern
Explanation of Builder Design Pattern’s Class Diagram
Builderis the base class which all the
Directorholds a reference to a
Directordirects,i.e. is responsible for, creation of the product using the interface provided to it by
ConcreteBuilderis responsible for a variant of the product. While
buildPart()method triggers the creation of sub-part, the
getResult()method is provided to fetch the final assembled product.
Productobject represents the complex final product which is being constructed.
Builder Design Pattern in Java – Class Diagram
The Java class diagram above depicts Builder Design pattern implemented for a Document Creation Engine. Lets quickly go through whats there in Java’s example’s class diagram & corresponding code –
Documentis the base class for all documents.
XMLDocumentare two types of Documents.
DocBuilderis the base class for Builders. It defines the methods which
DocCreationEngineinvokes to create a document.
XMLDocBuilderare concrete builders for PDF and XML Documents respectively.
DocCreationengine is the Director, i.e. it invokes the methods on
DocBuilderinstance passed to and creates the required document.
DocCreationEnginewith the specific
DocBuilderconcrete class depending on the type of document it wants to generate.
- Once the document has been created,
getDocument()method on the concrete builder instance to get the PDF or XML document.
- Output shows the sequence of creation of PDF and XML Documents.
In the above tutorial we understood what is Builder design pattern and the main scenarios in which it is applicable. We then looked at the UML class diagram for Builder Design Pattern & its explanation, a Java Use Case implementing Builder pattern with its class diagram and code for the classes shown in the class diagram, followed by explanation of both the class diagram & code. This concludes the tutorial on Builder design pattern.