Delegates in Action -Help
Posted
by Amutha
on Stack Overflow
See other posts from Stack Overflow
or by Amutha
Published on 2010-04-16T10:44:39Z
Indexed on
2010/04/16
11:53 UTC
Read the original article
Hit count: 298
I am learning delegates.I am very curious to apply delegates to the following chain-of-responsibility pattern.
Kindly help me the way to apply delegates to the following piece.
Thanks in advance.Thanks for your effort.
#region Chain of Responsibility Pattern
namespace Chain
{
public class Player
{
public string Name { get; set; }
public int Score { get; set; }
}
public abstract class PlayerHandler
{
protected PlayerHandler _Successor = null;
public abstract void HandlePlayer(Player _player);
public void SetupHandler(PlayerHandler _handler)
{
_Successor = _handler;
}
}
public class Employee : PlayerHandler
{
public override void HandlePlayer(Player _player)
{
if (_player.Score <= 100)
{
MessageBox.Show(string.Format("{0} is greeted
by Employee", _player.Name));
}
else
{
_Successor.HandlePlayer(_player);
}
}
}
public class Supervisor : PlayerHandler
{
public override void HandlePlayer(Player _player)
{
if (_player.Score >100 && _player.Score<=200)
{
MessageBox.Show(string.Format("{0} is greeted
by Supervisor", _player.Name));
}
else
{
_Successor.HandlePlayer(_player);
}
}
}
public class Manager : PlayerHandler
{
public override void HandlePlayer(Player _player)
{
if (_player.Score > 200)
{
MessageBox.Show(string.Format("{0} is
greeted by Manager", _player.Name));
}
else
{
MessageBox.Show(string.Format("{0} got low score",
_player.Name));
}
}
}
}
#endregion
#region Main()
void Main()
{
Chain.Player p1 = new Chain.Player();
p1.Name = "Jon";
p1.Score = 100;
Chain.Player p2 = new Chain.Player();
p2.Name = "William";
p2.Score = 170;
Chain.Player p3 = new Chain.Player();
p3.Name = "Robert";
p3.Score = 300;
Chain.Employee emp = new Chain.Employee();
Chain.Manager mgr = new Chain.Manager();
Chain.Supervisor sup = new Chain.Supervisor();
emp.SetupHandler(sup);
sup.SetupHandler(mgr);
emp.HandlePlayer(p1);
emp.HandlePlayer(p2);
emp.HandlePlayer(p3);
}
#endregion
© Stack Overflow or respective owner