Builder pattern

1

The builder pattern is a design pattern that provides a flexible solution to various object creation problems in object-oriented programming. The builder pattern separates the construction of a complex object from its representation. It is one of the 23 classic design patterns described in the book Design Patterns and is sub-categorized as a creational pattern.

Overview

The builder design pattern solves problems like: Creating and assembling the parts of a complex object directly within a class is inflexible. It commits the class to creating a particular representation of the complex object and makes it impossible to change the representation later independently from (without having to change) the class. The builder design pattern describes how to solve such problems: A class (the same construction process) can delegate to different objects to create different representations of a complex object.

Definition

The intent of the builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.

Advantages

Advantages of the builder pattern include:

Disadvantages

Disadvantages of the builder pattern include:

Structure

UML class and sequence diagram

In the above UML class diagram, the class doesn't create and assemble the and objects directly. Instead, the refers to the interface for building (creating and assembling) the parts of a complex object, which makes the independent of which concrete classes are instantiated (which representation is created). The class implements the interface by creating and assembling the and objects. The UML sequence diagram shows the run-time interactions: The object calls on the object, which creates and assembles the object. Thereafter, the calls on , which creates and assembles the object.

Class diagram

Examples

A C# example: The Director assembles a bicycle instance in the example above, delegating the construction to a separate builder object that has been given to the Director by the Client.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

View original