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 f8b46cb

Browse files
refactor builder and update description
1 parent 01a59f2 commit f8b46cb

File tree

3 files changed

+91
-12
lines changed

3 files changed

+91
-12
lines changed

‎builder.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Product class
2+
class Computer:
3+
def __init__(self, CPU, RAM, storage, GPU):
4+
self.CPU = CPU
5+
self.RAM = RAM
6+
self.storage = storage
7+
self.GPU = GPU
8+
9+
10+
# Builder interface
11+
class ComputerBuilder:
12+
def build_CPU(self):
13+
pass
14+
15+
def build_RAM(self):
16+
pass
17+
18+
def build_storage(self):
19+
pass
20+
21+
def build_GPU(self):
22+
pass
23+
24+
def get_computer(self):
25+
pass
26+
27+
28+
# Concrete builder
29+
class HighEndComputerBuilder(ComputerBuilder):
30+
def __init__(self):
31+
self.computer = Computer("Intel i9", "32GB", "1TB SSD",
32+
"NVIDIA RTX 3090")
33+
34+
def build_CPU(self):
35+
self.computer.CPU = "Intel i9"
36+
37+
def build_RAM(self):
38+
self.computer.RAM = "64GB"
39+
40+
def build_storage(self):
41+
self.computer.storage = "2TB SSD"
42+
43+
def build_GPU(self):
44+
self.computer.GPU = "NVIDIA RTX 3090"
45+
46+
def get_computer(self):
47+
return self.computer
48+
49+
50+
# Director
51+
class ComputerDirector:
52+
def __init__(self, builder):
53+
self.builder = builder
54+
55+
def construct_computer(self):
56+
self.builder.build_CPU()
57+
self.builder.build_RAM()
58+
self.builder.build_storage()
59+
self.builder.build_GPU()
60+
61+
62+
# Client code
63+
builder = HighEndComputerBuilder()
64+
director = ComputerDirector(builder)
65+
director.construct_computer()
66+
computer = builder.get_computer()
67+
68+
# Output
69+
print("Computer specs:")
70+
print(f"CPU: {computer.CPU}")
71+
print(f"RAM: {computer.RAM}")
72+
print(f"Storage: {computer.storage}")
73+
print(f"GPU: {computer.GPU}")

‎factory.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ def create_pizza(self, pizza_type):
6464
if __name__ == "__main__":
6565
# Creating a Margherita Pizza using the factory
6666
factory = PizzaFactory()
67-
margherita_pizza = factory.create_pizza('Margherita')
67+
pizza = factory.create_pizza('Margherita')
6868

6969
# Using the created Margherita Pizza
70-
margherita_pizza.prepare()
71-
margherita_pizza.bake()
72-
margherita_pizza.cut()
73-
margherita_pizza.box()
70+
pizza.prepare()
71+
pizza.bake()
72+
pizza.cut()
73+
pizza.box()
7474

7575
# Creating a Pepperoni Pizza using the factory
76-
pepperoni_pizza = factory.create_pizza('Pepperoni')
76+
pizza = factory.create_pizza('Pepperoni')
7777

7878
# Using the created Pepperoni Pizza
79-
pepperoni_pizza.prepare()
80-
pepperoni_pizza.bake()
81-
pepperoni_pizza.cut()
82-
pepperoni_pizza.box()
79+
pizza.prepare()
80+
pizza.bake()
81+
pizza.cut()
82+
pizza.box()

‎readme.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,21 @@ 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

4444

45-
## Singleton pattern
45+
## Singleton Pattern
4646

4747

4848
The singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance.
4949
One of the well-known "Gang of Four" design patterns.
5050
A singleton implementation may use lazy initialization in which the instance is created when the static method is first invoked.
5151

52-
## Monostate pattern
52+
## Monostate Pattern
5353

5454
The monostate pattern will create instances or objects with their own identity that all share the same internal state.
5555
It is often referred to as the Borg pattern in reference to The Borg in Star Trek.
5656
While being individuals in their own right, they all share the same collective consciousness or shared state.
57+
58+
## Factory Pattern
59+
60+
The Factory Pattern is to define an interface for creating an object, but leave the choice of its type to the subclasses,
61+
deferring the instantiation to the subclasses. Objects are created by calling a factory method
62+
instead of calling a constructor.

0 commit comments

Comments
(0)

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