Hi all
I have being looking through the book "Design Patterns" by the Gang of Four, I came across the composite pattern and implemented a sample app the demonstrates the pattern working (hopefully I did it correctly).
After some further reading using the web other resources, I came across a statement that said that a child in the pattern could have multiple parents. I have being trying to figure out how to adjust my sample app to accomidate this; I considered adding an list to the child, which allowed it to store a reference to all its parents, and the same for the parent, so that it could store all its children.
I am not sure if this is the best approach, can anyone suggest any alternative?
Here is the code that I am using in my basic composite pattern:
The Abstract child
The Child
The parent
And finally the main
Mike55.
I have being looking through the book "Design Patterns" by the Gang of Four, I came across the composite pattern and implemented a sample app the demonstrates the pattern working (hopefully I did it correctly).
After some further reading using the web other resources, I came across a statement that said that a child in the pattern could have multiple parents. I have being trying to figure out how to adjust my sample app to accomidate this; I considered adding an list to the child, which allowed it to store a reference to all its parents, and the same for the parent, so that it could store all its children.
I am not sure if this is the best approach, can anyone suggest any alternative?
Here is the code that I am using in my basic composite pattern:
The Abstract child
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace CompositePattern
{
abstract class AbstractChild : CompositePattern.IAbstractChild
{
protected string name;
public AbstractChild(string name)
{
this.name = name;
}
public abstract void Add(AbstractChild soldier);
public abstract void Remove(AbstractChild soldier);
public abstract void Display(int indent);
}
}
The Child
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace CompositePattern
{
class Child: AbstractChild
{
public Child(string name):base(name)
{
}
public override void Add(AbstractChild soldier)
{
Console.WriteLine("Cannot add to a child.");
//(throw new Exception("The method or operation is not implemented.");
}
public override void Remove(AbstractChild soldier)
{
Console.WriteLine("Cannot remove from a child.");
//throw new Exception("The method or operation is not implemented.");
}
public override void Display(int indent)
{
Console.WriteLine(new string('-', indent) + " " + name);
//throw new Exception("The method or operation is not implemented.");
}
}
}
The parent
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace CompositePattern
{
class Parent: AbstractChild
{
//private ArrayList elements = new ArrayList();
private List<AbstractChild> elements = new List<AbstractChild>();
public Parent(string name): base(name)
{
}
public override void Add(AbstractChild soldier)
{
elements.Add(soldier);
//throw new Exception("The method or operation is not implemented.");
}
public override void Remove(AbstractChild soldier)
{
elements.Remove(soldier);
//throw new Exception("The method or operation is not implemented.");
}
public override void Display(int indent)
{
Console.WriteLine(new string('-', indent) + " + " + name);
foreach (AbstractChild aSoldier in elements)
{
aSoldier.Display(indent + 2);
}
//throw new Exception("The method or operation is not implemented.");
}
}
}
And finally the main
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace CompositePattern
{
class MainApp
{
public static void Main()
{
Parent myParent = new Parent("John Doe");
myParent.Add(new Child("Tom"));
myParent.Add(new Child("Dick"));
myParent.Add(new Child("Harry"));
myParent.Display(0);
Console.Read();
}
}
}
Mike55.