Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created July 16, 2013 12:19
Show Gist options
  • Save yemrekeskin/6008201 to your computer and use it in GitHub Desktop.
Save yemrekeskin/6008201 to your computer and use it in GitHub Desktop.
Composition Design Pattern
public abstract class Staff
{
protected string Title;
public Staff(string title)
{
this.Title = title;
}
public abstract void Add(Staff staff);
public abstract void Remove(Staff staff);
public abstract void ShowProfile();
}
public class LeafStaff
:Staff
{
public LeafStaff(string title)
:base(title)
{
}
public override void ShowProfile()
{
Console.WriteLine(" - Leaf : {0}", this.Title);
}
public override void Add(Staff staff)
{
Console.WriteLine("Cannot add to a LeafStaff");
}
public override void Remove(Staff staff)
{
Console.WriteLine("Cannot add to a LeafStaff");
}
}
public class CompositionStaff
:Staff
{
private List<Staff> leafs = new List<Staff>();
public CompositionStaff(string title)
:base(title)
{
}
public override void ShowProfile()
{
Console.WriteLine("+ Composition : {0}", this.Title);
// Display each child element on this node
foreach (Staff item in leafs)
{
item.ShowProfile();
}
}
public override void Add(Staff staff)
{
leafs.Add(staff);
}
public override void Remove(Staff staff)
{
leafs.Remove(staff);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment