using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace corSample2
{
class Program
{
static void Main(string[] args)
{
AdAction action1, action2, action3;
action1 = new AxisAction("上料");
action2 = new IoAction("开灯");
action3 = new AxisAction("下料");
//职责链建立
action1.SetSuccessor(action2);
action2.SetSuccessor(action3);
//执行开始
action1.Process();
Console.ReadKey();
}
public static bool bStop = false;
/// <summary>
/// 抽象处理者
/// </summary>
abstract class AdAction
{
protected AdAction successor;
protected string name;
public AdAction(string name)
{
this.name = name;
}
public void SetSuccessor(AdAction successor)
{
this.successor = successor;
}
public abstract void Process();
}
/// <summary>
/// 具体处理者轴
/// </summary>
class AxisAction:AdAction
{
public AxisAction(string name):base(name)
{
}
public override void Process()
{
if (bStop) return;
Console.WriteLine($"轴{name}开始运动");
if (this.successor != null)
successor.Process();
}
}
/// <summary>
/// 具体处理者IO
/// </summary>
class IoAction : AdAction
{
public IoAction(string name) : base(name)
{
}
public override void Process()
{
if (bStop) return;
Console.WriteLine($"IO{name}开关执行");
if (this.successor != null)
successor.Process();
}
}
}
}
结果