Home > Articles > Programming > C#

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

This chapter is from the book

The Employee Classes

We could now imagine representing the company as a Composite made up of nodes: managers and employees. It would be possible to use a single class to represent all employees, but since each level may have different properties, it might be more useful to define at least two classes: Employees and Bosses. Employees are leaf nodes and cannot have employees under them. Bosses are nodes that may have employee nodes under them.

We'll start with the AbstractEmployee class and derive our concrete Employee classes from it.

 public interface AbstractEmployee   {
    float getSalary();        //get current salary
    string getName();        //get name
    bool isLeaf();          //true if leaf
    void add(string nm, float salary);   //add subordinate
    void add(AbstractEmployee emp);     //add subordinate
    IEnumerator getSubordinates();     //get subordinates
    AbstractEmployee getChild();      //get child
    float getSalaries();       //get sum of salaries

In C# we have a built-in enumeration interface called IEnumerator. This interface consists of these methods.

 bool MoveNext();      //False if no more left
 object Current()      //get current object
 void Reset();       //move to first 

So we can create an AbstractEmployee interface that returns an Enumerator. You move through an enumeration, allowing for the fact that it might be empty, using the following approach.

 while (e.MoveNext()) {
  Emp = (Employee)e.Current();
  //..do computation..

This Enumerator may, of course, be empty and can thus be used for both nodes and leaves of the composite.

Our concrete Employee class will store the name and salary of each employee and allow us to fetch them as needed.

 public class Employee :AbstractEmployee    {
    protected float salary;
    protected string name;
    protected ArrayList subordinates;
    public Employee(string nm, float salary)      {
       subordinates = new ArrayList();
       name = nm;
       salary = salry;
    public float getSalary() {
       return salary;
    public string getName() {
       return name;
    public bool isLeaf() {
       return subordinates.Count == 0;
    public virtual AbstractEmployee getChild() {
       return null;

The Employee class must have concrete implementations of the add, remove, getChild, and subordinates classes. Since an Employee is a leaf, all of these will return some sort of error indication. The subordinates method could return a null, but programming will be more consistent if subordinates returns an empty enumeration.

 public IEnumerator getSubordinates() {
    return subordinates.GetEnumerator ();

The add and remove methods must generate errors, since members of the basic Employee class cannot have subordinates. We throw an Exception if you call these methods in the basic Employee class.

 public virtual void add(string nm, float salary) {
  throw new Exception(
    "No subordinates in base employee class");
 public virtual void add(AbstractEmployee emp) {
    throw new Exception(
    "No subordinates in base employee class");

The Boss Class

Our Boss class is a subclass of Employee and allows us to store subordinate employees as well. We'll store them in an ArrayList called subordinates and return them through an enumeration. Thus, if a particular Boss has temporarily run out of Employees, the enumeration will just be empty.

 public class Boss:Employee {
    public Boss(string name, float salary):base(name,salary) {}
    public override void add(string nm, float salary) {
       AbstractEmployee emp = new Employee(nm,salary);
       subordinates.Add (emp);
    public override void add(AbstractEmployee emp){

If you want to get a list of employees of a given supervisor, you can obtain an Enumeration of them directly from the ArrayList. Similarly, you can use this same ArrayList to return a sum of salaries for any employee and his or her subordinates.

 public float getSalaries() {
    float sum;
    AbstractEmployee esub;
    //get the salaries of the boss and subordinates
    sum = getSalary();
    IEnumerator enumSub = subordinates.GetEnumerator() ;
    while (enumSub.MoveNext()) {
       esub = (AbstractEmployee)enumSub.Current;
       sum += esub.getSalaries();
  return sum;

Note that this method starts with the salary of the current Employee and then calls the getSalaries() method on each subordinate. This is, of course, recursive, and any employees who have subordinates will be included. A diagram of these classes is shown in Figure 16-2.

Figure 16-2Figure 16-2. The AbstractEmployee class and how Employee and Boss are derived from it

  • + Share This
  • 🔖 Save To Your Account


comments powered by Disqus