Skip to content

Commit ca7a0a5

Browse files
committed
refactor buider, add Budget model
1 parent 8d756e7 commit ca7a0a5

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

builder.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ def __str__(self):
6666
+ str(self.computer.storage)) + ","
6767
+ str(self.computer.Screen))
6868

69+
class BudgetComputerBuilder(ComputerBuilder):
70+
def __init__(self):
71+
self.computer = Computer("Intel i3", "8GB", "250MB HD",
72+
"NVIDIA",
73+
screen="13 Inch")
74+
75+
def build_cpu(self):
76+
self.computer.CPU = "Intel i3"
77+
78+
def build_ram(self):
79+
self.computer.RAM = "8GB"
80+
81+
def build_storage(self):
82+
self.computer.storage = "250MB HD"
83+
84+
def build_gpu(self):
85+
self.computer.GPU = "NVIDIA"
86+
87+
def build_screen(self):
88+
self.computer.Screen = "13 Inch"
89+
90+
def get_computer(self):
91+
return self.computer
92+
93+
def __str__(self):
94+
return (((f"\nHigh End Computer : " + str(self.computer.CPU) +
95+
"," + str(self.computer.RAM)) + ","
96+
+ str(self.computer.storage)) + ","
97+
+ str(self.computer.Screen))
98+
6999

70100
# Director
71101
class ComputerDirector:
@@ -86,10 +116,25 @@ def construct_computer(self):
86116
director.construct_computer()
87117
computer = builder.get_computer()
88118

89-
# Output
90-
print("Computer specs:")
91-
print(f"CPU: {computer.CPU}")
92-
print(f"RAM: {computer.RAM}")
93-
print(f"Storage: {computer.storage}")
94-
print(f"GPU: {computer.GPU}")
95-
print(f"Screen: {computer.Screen}")
119+
120+
def print_spec(model: str, computer: Computer) -> None:
121+
print(f"\nComputer specs: {model}")
122+
print(f"CPU: {computer.CPU}")
123+
print(f"RAM: {computer.RAM}")
124+
print(f"Storage: {computer.storage}")
125+
print(f"GPU: {computer.GPU}")
126+
print(f"Screen: {computer.Screen}")
127+
128+
129+
# Output for a High End model
130+
print_spec("High End", computer)
131+
132+
# Client code
133+
builder = BudgetComputerBuilder()
134+
director = ComputerDirector(builder)
135+
director.construct_computer()
136+
computer = builder.get_computer()
137+
138+
# Output for a Budget model
139+
print_spec("Budget", computer)
140+

0 commit comments

Comments
 (0)