我正在学习代表。我非常好奇地将代表应用于以下责任链模式。
请帮助我将代表申请到下一篇文章。
提前致谢。谢谢您的努力。
#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
答案 0 :(得分:1)
考虑以下示例:
public class Player
{
private readonly string _name;
private readonly string _surname;
private readonly int _score;
private string _nickName = "";
public Player(string name, string surname, int score)
{
_name = name;
_surname = surname;
_score = score;
}
public string FullName
{
get
{
if (string.IsNullOrEmpty(_nickName)) return string.Format("{0} {1}", _name, _surname);
else return string.Format("{0} \"{1}\" {2}", _name, _nickName, _surname);
}
}
public int Score
{
get { return _score; }
}
public string NickName
{
get { return _nickName; }
set { _nickName = value; }
}
}
public delegate bool PlayerHandlerDelegate(Player player);
public class HandlersChain
{
private readonly List<PlayerHandlerDelegate> _chain = new List<PlayerHandlerDelegate>();
public void AddHandler(PlayerHandlerDelegate handler)
{
_chain.Add(handler);
}
public void HandlePlayer(Player player)
{
foreach (PlayerHandlerDelegate handler in _chain)
{
if (handler(player)) break;
}
}
}
class Program
{
static PlayerHandlerDelegate CreateHandlerDelegate(string position, int minScore)
{
return delegate(Player p)
{
if (p.Score > minScore)
{
//MessageBox.Show(string.Format("{0} is greeted by {1}", p.FullName, position));
Console.WriteLine(string.Format("{0} is greeted by {1}", p.FullName, position));
return true;
}
return false;
};
}
static void Main(string[] args)
{
Player p1 = new Player("John", "Smith", 100);
Player p2 = new Player("William", "Brown", 170);
Player p3 = new Player("Robert", "Johns", 500);
HandlersChain chain = new HandlersChain();
chain.AddHandler(delegate(Player p)
{
if (p.Score > 400)
{
p.NickName = "The Hero";
}
return false;
});
chain.AddHandler(CreateHandlerDelegate("Manager", 300));
chain.AddHandler(CreateHandlerDelegate("Supervisor", 200));
chain.AddHandler(CreateHandlerDelegate("Employee", 100));
chain.AddHandler(delegate(Player p)
{
if (p.Score <= 100)
{
//MessageBox.Show(string.Format("{0} got low score", p.FullName));
Console.WriteLine(string.Format("{0} got low score", p.FullName));
return true;
}
return false;
});
chain.HandlePlayer(p1);
chain.HandlePlayer(p2);
chain.HandlePlayer(p3);
}
}
我使用了Console应用程序,因此MessageBox更改为Console。
这里的原始设计略有改变。现在有一个包含处理程序集合的链对象。处理程序彼此不了解。 Chain按添加顺序调用它们。如果玩家被“完全处理”,即如果不应该调用链中的其他处理程序,则每个处理程序返回true。从理论上讲,如果处理程序做了一些工作,则不需要处理程序返回true(即停止处理)。这取决于潜在的逻辑。您还可以看到所有“典型”委托都是通过使用闭包的特殊方法创建的(请参阅http://en.wikipedia.org/wiki/Closure_(computer_science),遗憾的是链接“(”如果添加为链接似乎会被破坏)。
P.S。我还在使用VS 2005,所以在我的样本中没有像lambda这样的时髦东西。遗憾。