-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite-design-P.py
58 lines (39 loc) · 1.37 KB
/
composite-design-P.py
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
50
51
52
53
54
55
56
57
58
# Composite Design Pattern
from abc import ABCMeta, abstractmethod
class IDepartment(metaclass=ABCMeta):
@abstractmethod
def __init__(self, empoloyees):
""" Implement in child class"""
@staticmethod
def print_department():
""" Implement in chield class"""
class Accounting(IDepartment):
def __init__(self, employees):
self.employees = employees
def print_department(self):
print(f'Accounting Department: {self.employees}')
class Development(IDepartment):
def __init__(self, employees):
self.employees = employees
def print_department(self):
print(f'Development Department: {self.employees}')
class ParentDepartment(IDepartment):
def __init__(self, employees):
self.employees = employees
self.base_employees = employees
self.sub_depts = []
def add(self, dept):
self.sub_depts.append(dept)
self.employees += dept.employees
def print_department(self):
print('Parent Department')
print(f'Parent Department Base Employees: {self.base_employees}')
for dept in self.sub_depts:
dept.print_department()
print(f'Total number of employees: {self.employees}')
dept1 = Accounting(200)
dept2 = Development(170)
parent_dept = ParentDepartment(30)
parent_dept.add(dept1)
parent_dept.add(dept2)
parent_dept.print_department()