31 lines
1.3 KiB
Markdown
31 lines
1.3 KiB
Markdown
# Builder Design Pattern — Java Example
|
|
|
|
**Pattern:** Creational → Builder
|
|
**Article:** https://ankurm.com/builder-design-pattern-java/
|
|
|
|
## What this example shows
|
|
|
|
An immutable `HttpRequest` assembled step by step instead of through a constructor with a dozen optional parameters. `HttpRequest` is the product — built once, never mutated afterward. `HttpClientConfig` is a director-style helper that encapsulates common request configurations so callers don't repeat the same builder calls. `Main` wires it together and shows the fluent builder chain in action.
|
|
|
|
## How to run
|
|
|
|
```bash
|
|
javac builder/*.java -d out/builder
|
|
java -cp out/builder builder.Main
|
|
```
|
|
|
|
Requires Java 25.
|
|
|
|
## Post Section ↔ File Mapping
|
|
|
|
| Post Section | File(s) |
|
|
|---|---|
|
|
| Part 1 — The Product: HttpRequest (Immutable) | `HttpRequest.java` |
|
|
| Part 2 — The Director: Encapsulating Common Configurations | `HttpClientConfig.java` |
|
|
| Part 3 — Using the Builder: Client Code | `Main.java` |
|
|
|
|
Note: the Lombok `@Builder` example and the JDK standard-library snippets (`HttpRequest.newBuilder()`, `StringBuilder`, `ProcessBuilder`) are illustrative only — they are not part of this repository's runnable example.
|
|
|
|
Article: https://ankurm.com/builder-design-pattern-java/
|
|
All patterns: https://ankurm.com/design-patterns-java/
|