forked from CatalinStefan/python-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite.py
More file actions
36 lines (27 loc) · 873 Bytes
/
Copy pathcomposite.py
File metadata and controls
36 lines (27 loc) · 873 Bytes
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
class Equipment:
def __init__(self, name: str, price: int):
self.name = name
self.price = price
class Composite:
def __init__(self, name: str):
self.name = name
self.items = []
def add(self, equipment: Equipment):
self.items.append(equipment)
return self
@property
def price(self):
return sum([x.price for x in self.items])
@price.setter
def price(self, value):
self.price = value
if __name__ == '__main__':
computer = Composite("PC")
processor = Equipment("Processor", 1000)
hard_drive = Equipment("Hard drive", 250)
memory = Composite("Memory")
rom = Equipment("Read only memory", 100)
ram = Equipment("Random access memory", 75)
mem = memory.add(rom).add(ram)
pc = computer.add(processor).add(hard_drive).add(memory)
print(pc.price)