-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite_pattern.py
More file actions
49 lines (38 loc) · 1.29 KB
/
composite_pattern.py
File metadata and controls
49 lines (38 loc) · 1.29 KB
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
from abc import *
class Component(ABC):
@abstractmethod
def get_size(self) -> int: ...
@abstractmethod
def display(self, indent: int = 0) -> None: ...
class File(Component):
def __init__(self, name: str, size: int):
self.name = name
self.size = size
def get_size(self) -> int:
return self.size
def display(self, indent: int = 0) -> None:
print(" " * indent + f"- {self.name} (file, {self.size} bytes)")
class Folder(Component):
def __init__(self, name: str):
self.name = name
self.children: list[Component] = []
def add(self, comp: Component) -> None:
self.children.append(comp)
def remove(self, comp: Component) -> None:
self.children.remove(comp)
def get_size(self) -> int:
total = 0
for c in self.children:
total += c.get_size()
return total
def display(self, indent: int = 0) -> None:
print(" " * indent + f"+ {self.name} (folder, {self.get_size()} bytes total)")
for c in self.children:
c.display(indent + 2)
if __name__ == '__main__':
root = Folder("root")
root.add(File("a.txt", 100))
root.add(File("b.txt", 200))
print("== tree ==")
root.display()
print("size:", root.get_size())