// The main program static void Main(string[] args) { Human fred = new Human("Fred"); Human bob = new Human("Bob"); Ogre bill = new Ogre("Bill"); Building castle = new Building(); fred.SayHello(); bill.SayHello(); castle.AssignGuard(fred); castle.AssignGuard(bob); // Make the guards patrol // the castle castle.StartPatrol(); // Give em lunch castle.LunchBreak(); // Demo pickup a new spell fred.CastSpell(); // No spell fred.PickUpSpell(new FireballSpell()); fred.CastSpell(); Console.ReadLine(); // Pause } // The parent class abstract class Character { // Attributes private string _name; // Constructors public Character(string name) { _name = name; } // Properties public string Name { get { return _name; } set { _name = value; } } // Methods // Polymorphism (i.e. virtual) // Allow child class (Human, Ogre, etc) to // provide a different method to this. public virtual void SayHello() { Console.WriteLine("Character {0} says hello", Name); } } // The child classes // Implement ISecurityGuard so Human can play the role // of a Security Guard. class Human:Character, ISecurityGuard // Inherit from Character { // Attributes private IMagicSpell _currentSpell; // Constructors public Human(string name) : base(name) // Give the character a name { // Human starts off with no spell // to cast, until they pick one // up!! _currentSpell = new NoSpell(); } // Properties // Methods public void EatDoughnuts() { Console.WriteLine("Human {0} is eating a doughnut", Name); } public void PatrolPerimeter() { Console.WriteLine("Human {0} is out on patrol", Name); } public void SearchForPlayer() { Console.WriteLine("Human {0} is searching for player", Name); } // STRATEGY DESIGN PATTERN // *********************** public void PickUpSpell(IMagicSpell spell) { // Store some Magic Spell we were // just passed _currentSpell = spell; } public void CastSpell() { // I have no idea what the spell was // as long as the object that was passed // in PickUpSpell() played the role of // a Magic Spell I don't care!! _currentSpell.CastSpell(); } } class Ogre:Character { // Attributes // Constructors public Ogre(string name) : base(name) // Give the character a name { } // Properties // Methods // Change the way the Ogre says Hello // Replace the SayHello() method // in the parent Character class. public override void SayHello() { Console.WriteLine("Ogre {0} says Hdjksdfakl!!", Name); } } // The interface -- the role of Security Guard interface ISecurityGuard // Actions reqd to play the role of a Security Guard { void EatDoughnuts(); void PatrolPerimeter(); void SearchForPlayer(); } // The building using the security guard class Building { // Attributes private List _guards; // Constructors public Building() { _guards = new List(); } // Properties // Methods public void AssignGuard(ISecurityGuard guard) { _guards.Add(guard); } public void StartPatrol() { foreach (ISecurityGuard guard in _guards) { guard.PatrolPerimeter(); } } public void LunchBreak() { foreach (ISecurityGuard guard in _guards) { guard.EatDoughnuts(); } } } // The Magic Spell interface -- all spells must play the role of a MagicSpell interface IMagicSpell { void CastSpell(); } // Some spells class NoSpell:IMagicSpell { public void CastSpell() { Console.WriteLine("I don't have a spell to cast"); } } class FireballSpell:IMagicSpell { public void CastSpell() { Console.WriteLine("Cast fireball spell"); } }