Posted Joe Chu cpp4 minutes read (About 585 words)0 visits
Builder Design Pattern
Design pattern.
1. Introduction
The Builder pattern typically consists of the following components:
Product: This is the complex object that needs to be constructed. It contains the actual data and behavior of the object.
Builder: This is an abstract interface or abstract class that defines the steps required to construct the Product object. It declares a set of methods for building different parts or aspects of the Product.
Concrete Builders: These are concrete implementations of the Builder interface or classes that provide the actual implementation for constructing the Product object. Each Concrete Builder constructs the Product differently, depending on the desired representation or configuration.
Director: The Director class is responsible for orchestrating the construction process by calling the appropriate methods on the Builder in the correct order. It knows the sequence of steps required to create the Product object.
2. Example
Let’s create camera objects using the Builder design pattern.
Different types of cameras, such as DSLRs, phone cameras, 360-degree cameras, etc., have various specifications, but their fundamental functionalities are similar. We can describe their specifications using common terms such as focus, white balance, stabilization, exposure time, and fps.
In this example:
The product is a camera.
The builder defines the basic camera specifications without providing detailed implementation.
The ConcreteBuilder determines the specific specifications each type of camera should have.
The Director is responsible for assembling a certain type of camera.