Composite Pattern

I have being going thought the book "Design Patterns - Elements of Reusable Object Oriented Software", and have begun working with the composite pattern.

I am trying to set-up the pattern so that a child can have multiple parents. According to the Gang of Four, the way to do this is to define the parent reference in the component class so that the leaf and the composite classes can inherit the reference and the operations that manage it.

To that effect, here is the code that I am using:

The Component

[code]

using System;

using System.Collections.Generic;

using System.Text;

namespace CompositePattern

{

abstractclassAbstractEmployee : CompositePattern.IAbstractEmployee

{

publicstring name;

publicstring title;

publicdecimal salary;

//This is a list of references for all my parents. See GoF pg166(Implementation pt1.) for reasoning.

publicList<AbstractEmployee> myParents =newList<AbstractEmployee>();

public AbstractEmployee(string name,string title,decimal salary)

{

this.name = name;

this.title = title;

this.salary = salary;

}

//Add a leaf to a parent, or add a parent to another parent.

publicabstractvoid Add(AbstractEmployee employee);

//Remove a leaf from a parent.

publicabstractvoid Remove(AbstractEmployee employee);

//Display all the leaves for a particular parent.

publicabstractvoid Display(int indent);

//Get the name of the parent/leaf.

publicabstractstring GetName();

//Get the salary of the parent/leaf.

publicabstractstring GetTitle();

//Get the salary of the parent/leaf.

publicabstractdecimal GetSalary();

//Add a new reference to the object for a new parent.

publicabstractvoid AddParentReference(AbstractEmployee myParentRef);

//List all the references to parents that an object has.

publicabstractvoid ListParentReference();

publicabstractvoid RemoveReferences(CompositeElement myParentRef);

}

}

[/code]

The Composite

[code]

using System;

using System.Collections.Generic;

using System.Text;

namespace CompositePattern

{

classCompositeElement:AbstractEmployee

{

List<AbstractEmployee> myElements =newList<AbstractEmployee>();

public CompositeElement(string name,string title,decimal salary):base(name, title, salary)

{

}

//Add an employee to the manger.

publicoverridevoid Add(AbstractEmployee myEmployee)

{

myElements.Add(myEmployee);

}

//Remove an employee from the manger.

publicoverridevoid Remove(AbstractEmployee myEmployee)

{

myElements.Remove(myEmployee);

}

//1. Begin by printing the name of the parent, 2. followed by its children.

publicoverridevoid Display(int indent)

{

//1. Print the name of the parnet.

Console.WriteLine(newstring('-', indent) +"+ " + name);

//2. Print all the children of the parent.

foreach (AbstractEmployee myEmployeein myElements)

{

myEmployee.Display(indent + 1);

}

}

//Return the name of a parent.

publicoverridestring GetName()

{

return name;

}

//Return the title of the parent.

publicoverridestring GetTitle()

{

return title;

}

//Return the salary of the parent.

publicoverridedecimal GetSalary()

{

return salary;

}

//Add a reference to the parent.

publicoverridevoid AddParentReference(AbstractEmployee myParentRef)

{

myParents.Add(myParentRef);

}

//List all the references that the parent has to other parents.

publicoverridevoid ListParentReference()

{

if (myParents.Count == 0)

{

Console.WriteLine("No references exist.");

}

else

{

//1. Print the name of the parnet.

Console.WriteLine(newstring('-', 1) +"+ " + name);

int counter = 0;

//Print all the references that the object has to parents.

foreach (AbstractEmployee myRefin myParents)

{

counter += 1;

Console.WriteLine("Reference " + counter +": " + myRef.GetName());

}

}

}

//Remove a reference to a parent.

publicoverridevoid RemoveReferences(CompositeElement myParentRef)

{

myParents.Remove(myParentRef);

}

}

}

[/code]

The Leaf

[code]

using System;

using System.Collections.Generic;

using System.Text;

namespace CompositePattern

{

classEmployee:AbstractEmployee

{

public Employee(string name,string title,decimal salary):base(name, title, salary)

{

}

publicoverridevoid Add(AbstractEmployee myEmployee)

{

Console.WriteLine("Cannot ADD to an employee, operation can only be performed on an manager.");

}

publicoverridevoid Remove(AbstractEmployee myEmployee)

{

Console.WriteLine("Cannot REMOVE from an employee, operation can only be performed on an manager.");

}

//Display the employee.

publicoverridevoid Display(int indent)

{

Console.WriteLine(newstring('-', indent) +" " + name);

}

//Get the name of the employee.

publicoverridestring GetName()

{

return name;

}

//Get the title of the employee.

publicoverridestring GetTitle()

{

return title;

}

//Get the salary of the employee.

publicoverridedecimal GetSalary()

{

return salary;

}

//Add a reference to the child.

publicoverridevoid AddParentReference(AbstractEmployee myParentRef)

{

//Console.WriteLine("Cannot ADD_PARENT_REFERENCE from an employee, operation can only be performed on an manager.");

myParents.Add(myParentRef);

}

//List all the references that the child has to other parents.

publicoverridevoid ListParentReference()

{

if (myParents.Count == 0)

{

Console.WriteLine("No references exist.");

}

else

{

//1. Print the name of the parnet.

Console.WriteLine(newstring('-', 1) +"+ " + name);

int counter = 0;

//Print all the references that the object has to parents.

foreach (AbstractEmployee myRefin myParents)

{

counter += 1;

Console.WriteLine("Reference " + counter +": " + myRef.GetName());

}

}

}

//Remove a reference to a parent.

publicoverridevoid RemoveReferences(CompositeElement myParentRef)

{

myParents.Remove(myParentRef);

}

}

}

[/code]

And finally, the code in the main class

[code]

//1. Create the primary parent.

CompositeElement root =newCompositeElement("PJimmy","Managing Director", 100000);

CompositeElement comp =newCompositeElement("PMichael","Developer", 17000);

//2. Create an employee and add a reference.

Employee emp =newEmployee("Tom","Quality Manager", 50000);

emp.AddParentReference(root);

emp.AddParentReference(comp);

comp.AddParentReference(root);

//Display the references.

emp.ListParentReference();

[/code]

Can anyone tell me if I am going about it the correct way, or am I doing it completely wrong.

Michael.

[22180 byte] By [MODonnell] at [2007-12-31]