DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Builder Design Pattern in Java
  • Design Pattern: What You Need to Know to Improve Your Code Effectively
  • Prototype Pattern in JavaScript
  • Object Relational Behavioral Design Patterns in Java

Trending

  • Setting Up Data Pipelines With Snowflake Dynamic Tables
  • How to Convert Between PDF and TIFF in Java
  • Start Coding With Google Cloud Workstations
  • Why I Started Using Dependency Injection in Python
  1. DZone
  2. Coding
  3. Languages
  4. Intro to Design Patterns: Builder Pattern

Intro to Design Patterns: Builder Pattern

By 
Andre Mare user avatar
Andre Mare
·
Apr. 15, 08 · News
Likes (0)
Comment
Save
Tweet
Share
75.8K Views

Join the DZone community and get the full member experience.

Join For Free
Either because you're new to them, or as a refresher, here is the start of a series on the "Gang of Four" design patterns. Andre Mare, the author of the Java Design Concepts blog, is a J2EE Specialist and has more than seven years experience in development and design of enterprise systems. Here he starts a series of articles that aim to introduce you to the "Gang of Four" design patterns. -- Geertjan Wielenga, JavaLobby Zone Leader

 

Intent of the Pattern

The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. - Gof

Type

Object Creational

Solution

The Builder Pattern simplifies the construction of complex objects by only specifying the type and content that the object requires. The construction process of the complex object will therefore allow for different representations of the complex object to be created by the different builders. Each of the concrete builder objects will construct a different representation of the complex object.

The Builder Pattern consists of a Builder, ConcreteBuilder, Director and Product.

  • The Director object is responsible for the construction process of the complex object but delegates the actual creation and assembly to the Builder interface.
  • The Builder object specifies the interface for creating parts of the complex object.
  • The Product represents the complex object that is created by the ConcreteBuilder objects. The Product consists of multiple parts that are created separately by the ConcreteBuilder objects.
  • The ConcreteBuilder objects create and assemble the parts that make up the Product through the Builder interface.

A client object creates an instance of the Director object and passes it the appropriate Builder object. The Director object invokes the methods on the Builder object to create and initialize specific parts of the Product object. The Builder receives content from the Director object and adds these to the Product object. The Client object has a reference to the Builder object and retrieves the created Product object from it.

Structure

Design Pattern, Builder Pattern, GOF

The construction of the Complex object (Product) is hidden by the Builder objects from the Client and Director objects. To change the internal representation of the complex object, a new concrete builder object is defined and used by the client through the Director object. Unlike other creational patterns, the Builder Pattern creates the complex object is sections through the Director and Builder objects. The Builder object may need access to information that was used in previous construction steps. This means that even thought the parts of a Product object is created in individual sections; they may interact with other sections to create the complex product object.

The complex Product objects do not usually have a shared abstract parent object, as their representation differ and a shared parent class or interface might not be possible. As the client object specify the Builder object, it should have the knowledge how to handle the product object that is created by the Builder object.

Java Sample Code

The example for the Builder Pattern is a meal that can be purchased at many fast food franchises. The complex Product is a combo meal that consists of a burger, beverage and a side order. The Builder objects are the different assistants at the till that knows how to create the combo meal for the client. The Director object is the instructions the client gives the assistant on how the specific order should be created.

Example Combo Meal:

Download Combo Meal Example

ComboMealClient.java
The ComboMealClient class makes use of the ComboMealDirector and the ComboMeal1ConcreteBuilder class to create a complex object called ComboMealProduct.

Code:

ComboMeal1ConcreteBuilder concreteBuilder = new ComboMeal1ConcreteBuilder();
ComboMealDirector mealDirector = new ComboMealDirector(concreteBuilder);
ComboMealProduct comboMealProduct = null;

mealDirector.constructComboMeal(SuperSize.HUGE);
comboMealProduct = concreteBuilder.getComboMealProduct();

ComboMealDirector.java
The ComboMealDirector class invokes the appropriate methods on the ConcreteBuilder (ComboMeal1ConcreteBuilder) to create a complex product ComboMealProduct.

Code:

public void constructComboMeal(SuperSize _mealSize) {
comboMealBuilder.buildBurgerPart();
comboMealBuilder.buildSideOrderPart(_mealSize);
comboMealBuilder.buildBeveragePart(_mealSize);
} // method constructComboMeal

ComboMealProduct.java
The ComboMealProduct class is the complex object whose individual parts is created by the different Builder objects.

ComboMealBuilder.java
The ComboMealBuilder class contains the interface that is used by the ComboMealDirector to create the complex object.

ComboMeal1ConcreteBuilder.java
The ComboMeal1ConcreteBuilder class contains implementation that is used by the ComboMealDirector to create the complex object.

Class Diagram Example

Design Pattern, Builder Pattern, GOF

Sequence Diagram

Builder Pattern - Sequence Diagram

Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler

References

  • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
    Design Patterns: Elements of Reusable Object-Oriented Software. Addison
    Wesley, 1995
Builder pattern Object (computer science) Design

Published at DZone with permission of Andre Mare. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Builder Design Pattern in Java
  • Design Pattern: What You Need to Know to Improve Your Code Effectively
  • Prototype Pattern in JavaScript
  • Object Relational Behavioral Design Patterns in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!