Design Patterns (GoF) (50 Qs)•Part 1: Creational & Structural Patterns (1 - 10)
What is the Builder Pattern and when should you use it?
junior LevelCode Exampledesign-patternsCreational
In Simple Words: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations:
- Use Case: Eliminates "Telescoping Constructors" where a class constructor has 10 parameters, most of which are optional:
typescript
const httpRequest = new HttpRequestBuilder()
.setUrl("https://api.com/users")
.setMethod("POST")
.setHeader("Authorization", "Bearer token")
.setBody({ name: "Alex" })
.setTimeout(5000)
.build();