-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPizzaBuilder.ts
154 lines (135 loc) · 3.61 KB
/
PizzaBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Interface for the PizzaBuilder.
* Defines methods for constructing a Pizza object step-by-step.
*/
interface PizzaBuilder {
/**
* Resets the builder to start creating a new pizza.
*/
reset(): void;
/**
* Sets the size of the pizza.
* @param {string} size - The size of the pizza (e.g., 'small', 'medium', 'large').
* @returns {PizzaBuilder} - The builder instance for method chaining.
*/
setSize(size: string): PizzaBuilder;
/**
* Sets the crust type of the pizza.
* @param {string} type - The type of crust (e.g., 'thin', 'thick', 'stuffed').
* @returns {PizzaBuilder} - The builder instance for method chaining.
*/
setCrustType(type: string): PizzaBuilder;
/**
* Adds a topping to the pizza.
* @param {string} topping - The topping to add (e.g., 'mushrooms', 'pepperoni').
* @returns {PizzaBuilder} - The builder instance for method chaining.
*/
addTopping(topping: string): PizzaBuilder;
/**
* Builds and returns the final Pizza object.
* @returns {Pizza} - The constructed Pizza object.
*/
build(): Pizza;
}
/**
* Represents a Pizza object with size, crust type, and toppings.
*/
class Pizza {
private toppings: string[] = [];
private size: string = '';
private crustType: string = '';
/**
* Sets the size of the pizza.
* @param {string} size - The size of the pizza.
*/
public setSize(size: string): void {
this.size = size;
}
/**
* Sets the crust type of the pizza.
* @param {string} type - The type of crust.
*/
public setCrustType(type: string): void {
this.crustType = type;
}
/**
* Adds a topping to the pizza.
* @param {string} topping - The topping to add.
*/
public addTopping(topping: string): void {
this.toppings.push(topping);
}
/**
* Provides a human-readable description of the pizza.
* @returns {string} - A description of the pizza.
*/
public describe(): string {
return `${this.size} ${
this.crustType
} crust pizza with ${this.toppings.join(', ')}`;
}
}
/**
* Builder class for creating custom Pizza objects.
*/
class CustomPizzaBuilder implements PizzaBuilder {
private pizza: Pizza;
/**
* Initializes a new instance of CustomPizzaBuilder.
*/
constructor() {
this.pizza = new Pizza();
}
/**
* Resets the builder to start creating a new pizza.
*/
public reset(): void {
this.pizza = new Pizza();
}
/**
* Sets the size of the pizza.
* @param {string} size - The size of the pizza.
* @returns {PizzaBuilder} - The builder instance for method chaining.
*/
public setSize(size: string): PizzaBuilder {
this.pizza.setSize(size);
return this;
}
/**
* Sets the crust type of the pizza.
* @param {string} type - The type of crust.
* @returns {PizzaBuilder} - The builder instance for method chaining.
*/
public setCrustType(type: string): PizzaBuilder {
this.pizza.setCrustType(type);
return this;
}
/**
* Adds a topping to the pizza.
* @param {string} topping - The topping to add.
* @returns {PizzaBuilder} - The builder instance for method chaining.
*/
public addTopping(topping: string): PizzaBuilder {
this.pizza.addTopping(topping);
return this;
}
/**
* Builds and returns the final Pizza object.
* @returns {Pizza} - The constructed Pizza object.
*/
public build(): Pizza {
const result = this.pizza;
this.reset();
return result;
}
}
// Example usage
const pizzaBuilder = new CustomPizzaBuilder();
const veggiePizza = pizzaBuilder
.setSize('large')
.setCrustType('thin')
.addTopping('mushrooms')
.addTopping('bell peppers')
.addTopping('onions')
.build();
console.log(veggiePizza.describe()); // Output: "large thin crust pizza with mushrooms, bell peppers, onions"