PATTERN
Builder
The Builder pattern is a creational design pattern that lets you construct complex objects step-by-step. It allows customization of the object being built without making the construction process itself complex and unmanageable. The pattern separates the construction of a complex object from its representation, so the same construction process can create different representations.
This pattern is useful when an object has multiple optional attributes, or when the construction process is complex and involves many steps. It addresses the problems that can arise when using traditional constructors to create complex objects, particularly telescoping constructors and the need for a separate object for configuration. It promotes code reusability and maintainability by encapsulating the construction logic.
Usage
The Builder pattern is commonly used where:
- Complex Object Creation: When constructing an object requires a sequence of steps and depends on various configuration options.
- Varied Representations: When you need to create different versions or types of an object using the same construction process.
- Avoiding Constructor Complexity: To avoid long and complicated constructors with numerous parameters.
- Immutable Objects: When you want to construct immutable objects, as the builder can assemble the object’s parts before final creation.
Examples
-
Java’s StringBuilder: The
StringBuilderclass in Java effectively implements the Builder pattern. You don’t construct a final string directly; instead, you use methods likeappend(),insert(), anddelete()to build up the string incrementally. Finally,toString()creates the immutableStringobject. This avoids the inefficiencies of repeatedly creating newStringobjects during modification. -
Python’s
datetimemodule: Constructing adatetimeobject in Python can be done directly withdatetime(year, month, day, hour, minute, second). However, thedatetime.datetimeclass also provides a builder-like interface through its various class methods (e.g.,datetime.now(),datetime.fromtimestamp()). These methods allow you to createdatetimeobjects with specific levels of detail, customizing the initialization process. -
Lombok
@BuilderAnnotation (Java): The Lombok library provides the@Builderannotation which generates a builder class for you automatically. This simplifies the use of the Builder pattern substantially and is seen in many Spring Boot projects where complex DTOs are used.