-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.py
61 lines (46 loc) · 1.2 KB
/
builder.py
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
from enum import Enum
class Color(Enum):
ROT = 1
GRUEN = 2
BLAU = 3
GELB = 4
ORANGE = 5
VIOLETT = 6
SCHWARZ = 7
WEISS = 8
class Vehicle:
def __init__(self) -> None:
self.make: str
self.model: str
self.color: Color
self.number_of_doors: int
def __str__(self):
return f"Vehicle({self.make}, {self.model}, {self.color.name}, {self.number_of_doors})"
class VehicleBuilder:
def __init__(self):
self.vehicle = Vehicle()
def set_make(self, make):
self.vehicle.make = make
return self
def set_model(self, model: str):
self.vehicle.model = model
return self
def set_color(self, color: Color):
self.vehicle.color = color
return self
def set_number_of_doors(self, number_of_doors):
self.vehicle.number_of_doors = number_of_doors
return self
def build(self):
return self.vehicle
# Verwendung des Builders
if __name__ == "__main__":
builder = VehicleBuilder()
car = (
builder.set_make("Volkswagen")
.set_model("Golf")
.set_color(Color.GELB)
.set_number_of_doors(4)
.build()
)
print(car)