Skip to content

Commit

Permalink
finished composition pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
xulww committed May 12, 2021
1 parent b1ddac6 commit 6d451b4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Composition/developer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from employee import Employee


class Developer(Employee):
_name = None
_salary = None

def __init__(self, name, salary):
self._name = name
self._salary = salary

def add(self, employee):
# leaf node cannot add employees
pass

def remove(self, employee):
# leaf node cannot remove employees
pass

def get_name(self):
return self._name

def get_salary(self):
return self._salary

def print_data(self):
print("Developer name: " + self.get_name() +
" with salary: " + str(self.get_salary()))
15 changes: 15 additions & 0 deletions Composition/employee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Employee:
def add(self, employee):
pass

def remove(self, employee):
pass

def get_name(self):
pass

def get_salary(self):
pass

def print_data(self):
pass
27 changes: 27 additions & 0 deletions Composition/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from manager import Manager
from developer import Developer


def main():
manager_one = Manager("Georgi", 4000)
manager_two = Manager("Tsvetomir", 5000)
manager_three = Manager("Spasimir The Boss", 7000)
developer_one = Developer("Atanas", 2500)
developer_two = Developer("Ivan", 3000)
developer_three = Developer("Petar", 3500)
developer_four = Developer("Anastas", 4000)
developer_five = Developer("Evgeni", 4000)

manager_one.add(developer_one)
manager_one.add(developer_two)
manager_two.add(developer_three)
manager_two.add(developer_four)
manager_three.add(developer_five)
manager_three.add(manager_one)
manager_three.add(manager_two)

manager_three.print_data()


if __name__ == "__main__":
main()
31 changes: 31 additions & 0 deletions Composition/manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from employee import Employee


class Manager(Employee):
_name = None
_salary = None
_employees = None

def __init__(self, name, salary):
self._name = name
self._salary = salary
self._employees = []

def add(self, employee):
self._employees.append(employee)

def remove(self, employee):
self._employees.remove(employee)

def get_name(self):
return self._name

def get_salary(self):
return self._salary

def print_data(self):
print("Manager name: " + self.get_name() +
" with salary: " + str(self.get_salary()))

for employee in self._employees:
employee.print_data()

0 comments on commit 6d451b4

Please sign in to comment.