Get employees under Manager

Problem statement: In an organisation display number of employees under each manager.

Solution: To solve this problem we will use Composite Design Pattern . The Composite pattern definition in wikipedia is “intent of a composite is to ‘compose’ objects into tree structures to represent part- whole hierarchies” .

Let’s start with the creation of interfaces we need :

  1. IEmployee : interface to be implemented by each employee.
  2. IManager: extends IEmployee as all Managers are also employee. Employees under a Manager can be Added or Removed.

Employee class: Display and GetEmployeeCount methods are virtual so that class inheriting Employee class can override these methods.

Manager: This class inherits Employee class and also implements IManager interface. It also overrides the virtual methods of Employee to modify its behavior.

Add some employees and display information in the Main method:

Output:

Download:

Complete source code is available at GitHub: EmployeeManagerApp