Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d8d63da

Browse files
refactor builder, tests and readme
1 parent 4d5de26 commit d8d63da

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

‎builder.py‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
# Product class
22
class Computer:
3-
def __init__(self, CPU, RAM, storage, GPU):
4-
self.CPU = CPU
5-
self.RAM = RAM
3+
def __init__(self, cpu, ram, storage, gpu, screen):
4+
self.CPU = cpu
5+
self.RAM = ram
66
self.storage = storage
7-
self.GPU = GPU
7+
self.GPU = gpu
8+
self.Screen = screen
9+
10+
def __str__(self):
11+
return (((f"\nHigh End Computer : " + str(self.CPU) +
12+
"," + str(self.RAM)) + ","
13+
+ str(self.storage)) + ","
14+
+ str(self.Screen))
815

916

1017
# Builder interface
1118
class ComputerBuilder:
12-
def build_CPU(self):
19+
def build_cpu(self):
1320
pass
1421

15-
def build_RAM(self):
22+
def build_ram(self):
1623
pass
1724

1825
def build_storage(self):
1926
pass
2027

21-
def build_GPU(self):
28+
def build_gpu(self):
29+
pass
30+
31+
def build_screen(self):
2232
pass
2333

2434
def get_computer(self):
@@ -29,34 +39,45 @@ def get_computer(self):
2939
class HighEndComputerBuilder(ComputerBuilder):
3040
def __init__(self):
3141
self.computer = Computer("Intel i9", "32GB", "1TB SSD",
32-
"NVIDIA RTX 3090")
42+
"NVIDIA Quadro",
43+
screen="17 Inch touch screen")
3344

34-
def build_CPU(self):
45+
def build_cpu(self):
3546
self.computer.CPU = "Intel i9"
3647

37-
def build_RAM(self):
48+
def build_ram(self):
3849
self.computer.RAM = "64GB"
3950

4051
def build_storage(self):
4152
self.computer.storage = "2TB SSD"
4253

43-
def build_GPU(self):
44-
self.computer.GPU = "NVIDIA RTX 3090"
54+
def build_gpu(self):
55+
self.computer.GPU = "NVIDIA Quadro"
56+
57+
def build_screen(self):
58+
self.computer.Screen = "17 Inch touch screen"
4559

4660
def get_computer(self):
4761
return self.computer
4862

63+
def __str__(self):
64+
return (((f"\nHigh End Computer : " + str(self.computer.CPU) +
65+
"," + str(self.computer.RAM)) + ","
66+
+ str(self.computer.storage)) + ","
67+
+ str(self.computer.Screen))
68+
4969

5070
# Director
5171
class ComputerDirector:
5272
def __init__(self, builder):
5373
self.builder = builder
5474

5575
def construct_computer(self):
56-
self.builder.build_CPU()
57-
self.builder.build_RAM()
76+
self.builder.build_cpu()
77+
self.builder.build_ram()
5878
self.builder.build_storage()
59-
self.builder.build_GPU()
79+
self.builder.build_gpu()
80+
self.builder.build_screen()
6081

6182

6283
# Client code
@@ -71,3 +92,4 @@ def construct_computer(self):
7192
print(f"RAM: {computer.RAM}")
7293
print(f"Storage: {computer.storage}")
7394
print(f"GPU: {computer.GPU}")
95+
print(f"Screen: {computer.Screen}")

‎readme.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ While classes and encapsulation are generally recommended,
4141
it's essential to consider the specific requirements of your project.
4242
In some cases, using global variables might be appropriate, but it's crucial to be mindful of the potential drawbacks mentioned above. Striking a balance and following good design principles will contribute to more maintainable and scalable code.
4343

44-
4544
## Singleton Pattern
4645

4746

@@ -55,8 +54,19 @@ The monostate pattern will create instances or objects with their own identity t
5554
It is often referred to as the Borg pattern in reference to The Borg in Star Trek.
5655
While being individuals in their own right, they all share the same collective consciousness or shared state.
5756

57+
## Other Useful Patterns
58+
5859
## Factory Pattern
5960

6061
The Factory Pattern is to define an interface for creating an object, but leave the choice of its type to the subclasses,
6162
deferring the instantiation to the subclasses. Objects are created by calling a factory method
62-
instead of calling a constructor.
63+
instead of calling a constructor.
64+
65+
## Builder Pattern
66+
67+
The Builder Pattern is a creational design pattern used to construct complex objects step by step.
68+
It separates the construction of a complex object from its representation. i.e. keep the process of putting
69+
it together separate from how it's actually represented or used in the program. This way, the same
70+
construction process can create different representations.
71+
Imagine you have an order for a new laptop. You will pick the processor, ram, screen size, memory , mouse, warranty and many other options.
72+
The builder pattern will allow for many different laptop variations.

‎tests/test_builder.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
3+
from builder import HighEndComputerBuilder
4+
from factory import PizzaFactory, MargheritaPizza, PepperoniPizza, Pizza
5+
6+
7+
# Assume you have a PizzaFactory class and different Pizza subclasses (e.g.,
8+
# MargheritaPizza, PepperoniPizza)
9+
10+
class TestBuilder(unittest.TestCase):
11+
def test_create_HighEndComputer(self):
12+
builder = HighEndComputerBuilder()
13+
computer= builder.get_computer()
14+
self.assertEqual(str(builder), "\nHigh End Computer : Intel i9,32GB,1TB SSD,17 Inch touch screen")
15+
self.assertEqual(str(computer.CPU), "Intel i9")
16+
self.assertEqual(str(computer.RAM), "32GB")
17+
self.assertEqual(str(computer.storage), "1TB SSD")
18+
self.assertEqual(str(computer.GPU), "NVIDIA Quadro")
19+
self.assertEqual(str(computer.Screen), "17 Inch touch screen")
20+
21+
if __name__ == '__main__':
22+
unittest.main()

‎tests/test_factory.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_instantiation_abtract_class_error(self):
2525
with self.assertRaises(TypeError):
2626
# Attempt to instantiate the abstract class, which should raise
2727
# a TypeError
28-
instance = Pizza()
28+
abstract_class = Pizza()
2929

3030

3131
if __name__ == '__main__':

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /