public class Creature
{
public string Name;
public int Attack, Defense;
public Creature(string name, int attack, int defense)
{
Name = name;
Attack = attack;
Defense = defense;
}
public override string ToString()
{
return $"Name:{Name} Attack:{Attack} Defense:{Defense}";
}
}
public class CreatureModifier
{
protected Creature creature;
protected CreatureModifier next;
public CreatureModifier(Creature creature)
{
this.creature = creature;
}
public void Add(CreatureModifier m)
{
if (next != null)
{
next.Add(m);
}
else
{
next = m;
}
}
public virtual void Handle()
{
next?.Handle();
}
}
public abstract class AbstractHandle : IHandle
{
private IHandle _nextHandle;
public IHandle SetNext(IHandle handle)
{
this._nextHandle = handle;
return handle;
}
public virtual object Handle(object request)
{
if (this._nextHandle != null)
{
return this._nextHandle.Handle(request);
}
else
{
return null;
}
}
}
在定义几个具体的职责链上处理的具体类:
public class MonkeyHandle : AbstractHandle
{
public override object Handle(object request)
{
if (request.ToString() == "Banana")
{
return $"Monkey: I'll eat the {request.ToString()}.\n";
}
else
{
return base.Handle(request);
}
}
}
public class SquirrelHandler : AbstractHandle
{
public override object Handle(object request)
{
if (request.ToString() == "Nut")
{
return $"Squirrel: I'll eat the {request.ToString()}.\n";
}
else
{
return base.Handle(request);
}
}
}
public class DogHandler : AbstractHandle
{
public override object Handle(object request)
{
if (request.ToString() == "MeatBall")
{
return $"Dog: I'll eat the {request.ToString()}.\n";
}
else
{
return base.Handle(request);
}
}
}
再定义使用方法,参数为单个职责链参数:
public static void ClientCode(AbstractHandler handler)
{
foreach (var food in new List<string> { "Nut", "Banana", "Cup of coffee" })
{
Console.WriteLine($"Client: Who wants a {food}?");
var result = handler.Handle(food);
if (result != null)
{
Console.Write($" {result}");
}
else
{
Console.WriteLine($" {food} was left untouched.");
}
}
}
现在定义流程处理链:
// The other part of the client code constructs the actual chain.
var monkey = new MonkeyHandler();
var squirrel = new SquirrelHandler();
var dog = new DogHandler();
monkey.SetNext(squirrel).SetNext(dog);
// The client should be able to send a request to any handler, not
// just the first one in the chain.
Console.WriteLine("Chain: Monkey > Squirrel > Dog\n");
ClientCode(monkey);
Console.WriteLine();
Console.WriteLine("Subchain: Squirrel > Dog\n");
ClientCode(squirrel);
输出结果为:
Chain: Monkey > Squirrel > Dog
Client: Who wants a Nut?
Squirrel: I'll eat the Nut.
Client: Who wants a Banana?
Monkey: I'll eat the Banana.
Client: Who wants a Cup of coffee?
Cup of coffee was left untouched.
Subchain: Squirrel > Dog
Client: Who wants a Nut?
Squirrel: I'll eat the Nut.
Client: Who wants a Banana?
Banana was left untouched.
Client: Who wants a Cup of coffee?
Cup of coffee was left untouched.
public class Creature
{
public string Name;
public int Attack, Defense;
public Creature(string name, int attack, int defense)
{
Name = name;
Attack = attack;
Defense = defense;
}
public override string ToString()
{
return $"Name:{Name} Attack:{Attack} Defense:{Defense}";
}
}
public class CreatureModifier
{
protected Creature creature;
protected CreatureModifier next;
public CreatureModifier(Creature creature)
{
this.creature = creature;
}
public void Add(CreatureModifier m)
{
if (next != null)
{
next.Add(m);
}
else
{
next = m;
}
}
public virtual void Handle()
{
next?.Handle();
}
}
public abstract class AbstractHandle : IHandle
{
private IHandle _nextHandle;
public IHandle SetNext(IHandle handle)
{
this._nextHandle = handle;
return handle;
}
public virtual object Handle(object request)
{
if (this._nextHandle != null)
{
return this._nextHandle.Handle(request);
}
else
{
return null;
}
}
}
在定义几个具体的职责链上处理的具体类:
public class MonkeyHandle : AbstractHandle
{
public override object Handle(object request)
{
if (request.ToString() == "Banana")
{
return $"Monkey: I'll eat the {request.ToString()}.\n";
}
else
{
return base.Handle(request);
}
}
}
public class SquirrelHandler : AbstractHandle
{
public override object Handle(object request)
{
if (request.ToString() == "Nut")
{
return $"Squirrel: I'll eat the {request.ToString()}.\n";
}
else
{
return base.Handle(request);
}
}
}
public class DogHandler : AbstractHandle
{
public override object Handle(object request)
{
if (request.ToString() == "MeatBall")
{
return $"Dog: I'll eat the {request.ToString()}.\n";
}
else
{
return base.Handle(request);
}
}
}
再定义使用方法,参数为单个职责链参数:
public static void ClientCode(AbstractHandler handler)
{
foreach (var food in new List<string> { "Nut", "Banana", "Cup of coffee" })
{
Console.WriteLine($"Client: Who wants a {food}?");
var result = handler.Handle(food);
if (result != null)
{
Console.Write($" {result}");
}
else
{
Console.WriteLine($" {food} was left untouched.");
}
}
}
现在定义流程处理链:
// The other part of the client code constructs the actual chain.
var monkey = new MonkeyHandler();
var squirrel = new SquirrelHandler();
var dog = new DogHandler();
monkey.SetNext(squirrel).SetNext(dog);
// The client should be able to send a request to any handler, not
// just the first one in the chain.
Console.WriteLine("Chain: Monkey > Squirrel > Dog\n");
ClientCode(monkey);
Console.WriteLine();
Console.WriteLine("Subchain: Squirrel > Dog\n");
ClientCode(squirrel);
输出结果为:
Chain: Monkey > Squirrel > Dog
Client: Who wants a Nut?
Squirrel: I'll eat the Nut.
Client: Who wants a Banana?
Monkey: I'll eat the Banana.
Client: Who wants a Cup of coffee?
Cup of coffee was left untouched.
Subchain: Squirrel > Dog
Client: Who wants a Nut?
Squirrel: I'll eat the Nut.
Client: Who wants a Banana?
Banana was left untouched.
Client: Who wants a Cup of coffee?
Cup of coffee was left untouched.