Created
July 16, 2013 12:19
-
-
Save yemrekeskin/6008201 to your computer and use it in GitHub Desktop.
Composition Design Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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