The Builder Pattern is a creational design pattern that constructs complex objects step by step. It allows you to create different types and representations of an object using the same construction code. This pattern is particularly useful when an object needs to be created with many optional parameters.
- Flexibility: Allows creating objects with different configurations using the same process
- Readability: Method chaining makes object creation more readable than constructors with many parameters
- Immutability: Objects can be made immutable after construction
- Validation: Construction process can include validation logic
- Optional Parameters: Handles objects with many optional parameters elegantly
-
Navigate to the src directory (parent of creational_design_patterns.builder_pattern):
cd src -
Compile all Java files:
javac creational_design_patterns.builder_pattern/example/*.java -
Run the example:
java creational_design_patterns.builder_pattern.example.BuilderPatternExample
The example demonstrates a Computer Builder that constructs computers with various specifications:
creational_design_patterns.builder_pattern/
├── example/
│ ├── Computer.java - Product class with nested ComputerBuilder
│ ├── ComputerDirector.java - Director class with predefined build methods
│ └── BuilderPatternExample.java - Main class demonstrating the pattern
└── README.md
- Product:
Computer(the complex object being built) - Builder:
Computer.ComputerBuilder(constructs the product step by step) - Director:
ComputerDirector(optional, provides predefined construction processes)
Computer computer = new Computer.ComputerBuilder("Intel i7", "16GB")
.setStorage("512GB SSD")
.setGraphicsCard("NVIDIA GTX 1660")
.enableWiFi()
.build();Computer gamingPC = ComputerDirector.buildGamingComputer();
Computer officePC = ComputerDirector.buildOfficeComputer();Computer basicPC = new Computer.ComputerBuilder("AMD Ryzen 5", "8GB")
.build(); // Uses default values for optional components- When creating objects with many optional parameters (avoids telescoping constructor problem)
- When you need different representations of the same object
- When the construction process should be independent of the parts that make up the object
- When you want to ensure objects are fully constructed before use
- When object creation is complex and requires step-by-step construction
Instead of:
// Confusing constructor with many parameters
Computer pc = new Computer("Intel i7", "16GB", "512GB SSD", "GTX 1660", "650W", true, false);Use Builder:
// Clear, readable builder pattern
Computer pc = new Computer.ComputerBuilder("Intel i7", "16GB")
.setStorage("512GB SSD")
.setGraphicsCard("GTX 1660")
.setPowerSupply("650W")
.enableWiFi()
.build();