Home > Articles > Programming > C#

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

This chapter is from the book


We can imagine cases where a simple Employee would stay in his current job but have new subordinates. For example, a Salesman might be asked to supervise sales trainees. For such a case, it is convenient to provide a method in the Boss class that creates a Boss from an Employee. We just provide an additional constructor that converts an employee into a boss.

 public Boss(AbstractEmployee emp):
       base(emp.getName() , emp.getSalary()) { 

Doubly Linked Lists

In the preceding implementation, we keep a reference to each subordinate in the Collection in each Boss class. This means that you can move down the chain from the president to any employee, but there is no way to move back up to find out who an employee's supervisor is. This is easily remedied by providing a constructor for each AbstractEmployee subclass that includes a reference to the parent node.

 public class Employee :AbstractEmployee    {
    protected float salary;
    protected string name;
    protected AbstractEmployee parent;
    protected ArrayList subordinates;
    public Employee(AbstractEmployee parnt,
           string nm, float salry)     {
       subordinates = new ArrayList();
       name = nm;
       salary = salry;
       parent = parnt;

Then you can quickly walk up the tree to produce a reporting chain.

 private void btShowBoss_Click(object sender, System.EventArgs e) {
    EmpNode node;
    node = (EmpNode)EmpTree.SelectedNode;
    AbstractEmployee emp = node.getEmployee ();
    string bosses = "";
    while(emp != null) {
       bosses += emp.getName () +"\n";
       emp = emp.getBoss();
    MessageBox.Show (null, bosses,"Reporting chain");

See Figure 16-4.

Figure 16-4Figure 16-4. The tree list display of the composite with a display of the parent nodes on the right

  • + Share This
  • 🔖 Save To Your Account