Builder Flashcards

(38 cards)

1
Q

What is the main purpose of the Builder Design Pattern?

A

To construct complex objects step by step while keeping the creation process separate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the Builder Pattern differ from the Factory Method and Abstract Factory Patterns?

A

Builder creates objects step-by-step, while Factory creates objects in one step.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is the Builder Pattern classified as a creational design pattern?

A

Because it focuses on object creation with flexible construction logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What problem does the Builder Pattern solve in software design?

A

It simplifies object creation with multiple parameters and optional configurations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does the Builder Pattern promote loose coupling?

A

The construction process is separate from the object’s representation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is the Builder Pattern useful when creating complex objects?

A

It allows gradual construction while maintaining code readability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does the Builder Pattern improve code readability and maintainability?

A

It eliminates long constructors and uses clear method calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When should you use the Builder Pattern instead of telescoping constructors?

A

When an object has many optional parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is meant by the term “step-by-step object creation” in the Builder Pattern?

A

The object is built progressively by calling different methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What would happen if an object with many optional parameters was built without using the Builder Pattern?

A

It would lead to long constructors and poor readability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the main components of the Builder Pattern?

A

Builder, Concrete Builder, Product, Director.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What role does the Builder play in the pattern?

A

It defines the steps for creating a product.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What role does the Concrete Builder play in the pattern?

A

It implements the steps defined by the Builder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a Product in the Builder Pattern?

A

The final complex object being built.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What role does the Director play in the Builder Pattern?

A

It orchestrates the steps to build a product.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the Builder ensure that an object is created in multiple steps?

A

By defining separate methods for each step.

17
Q

What happens if the Director class is omitted in the Builder Pattern?

A

The client must manually call all builder methods.

18
Q

How does method chaining improve the implementation of the Builder Pattern?

A

It allows fluent API design by returning this.

19
Q

Why does the Builder Pattern typically involve an interface or an abstract class?

A

To ensure flexibility and multiple builder implementations.

20
Q

What happens if a client directly accesses the Concrete Builder instead of the Director?

A

The client has more control but less abstraction.

21
Q

How would you apply the Builder Pattern in a system that generates complex reports?

A

Use different builders for PDF, Excel, and HTML reports.

22
Q

How would the Builder Pattern help in a game with character customization?

A

A CharacterBuilder can set weapons, armor, and skills step by step.

23
Q

How does the Builder Pattern improve the creation of SQL query objects?

A

It builds queries dynamically using method chaining.

24
Q

How would the Builder Pattern be useful in creating a house object with multiple optional parts?

A

It allows gradual house construction with optional features.

25
What are some disadvantages of using the Builder Pattern?
Increased complexity due to multiple classes. May be unnecessary for simple objects.
26
Give an example where the Builder Pattern would not be an ideal choice.
When an object has only a few parameters.
27
How does the Builder Pattern compare to using overloaded constructors for object creation?
Builder is more readable and scalable than multiple constructors.
28
How would the Builder Pattern be useful in an e-commerce system where customers can configure their own products?
It allows step-by-step product customization.
29
Can the Builder Pattern be used in multi-threaded applications? If so, what should be considered?
Yes, but ensure thread safety when modifying shared objects.
30
What is the Builder in the provided C++ code snippet? class Car { public: std::string engine; std::string color; int seats; void show() { std::cout << "Car with " << engine << " engine, " << color << " color, and " << seats << " seats.\n"; } }; class CarBuilder { public: virtual void setEngine(std::string engine) = 0; virtual void setColor(std::string color) = 0; virtual void setSeats(int seats) = 0; virtual Car* getCar() = 0; }; class SportsCarBuilder : public CarBuilder { private: Car* car; public: SportsCarBuilder() { car = new Car(); } void setEngine(std::string engine) override { car->engine = engine; } void setColor(std::string color) override { car->color = color; } void setSeats(int seats) override { car->seats = seats; } Car* getCar() override { return car; } }; class Director { public: void constructSportsCar(CarBuilder* builder) { builder->setEngine("V8"); builder->setColor("Red"); builder->setSeats(2); } };
CarBuilder class.
31
What is the Concrete Builder in the provided C++ code snippet?
SportsCarBuilder class.
32
What is the Product in the provided C++ code snippet?
Car class.
33
What is the Director in the provided C++ code snippet?
Director class.
34
How does this code follow the Builder Pattern?
It separates construction logic into Builder, Concrete Builder, and Director.
35
How would you modify the Builder Pattern to support multiple car types (Sedan, SUV, Truck)?
Create separate Concrete Builders for each car type.
36
What would happen if the Director class was removed from the Builder implementation?
The client must manually call builder methods.
37
What is the benefit of using method chaining in the Builder Pattern?
Fluent API for cleaner and more readable code.
38