开发应用-职责链模式(Chain Of Resposibility Pattern)
职责链模式(Chain Of Resposibility Pattern)
整理代码时找到一些好东西分享出来给大家学习,这是应用在RFID系统中读卡程序的接口。
职责链模式:
意图:适用于多个对象都有机会处理请求,从而避免请求的发送者或接受者
之间的耦合关系,将这些对象连成一条链,并沿着这条链传递该请求,
直到有一个对象处理它为止。
适用性:有多个对象可以处理一个请求,哪个对象处理该请求运行时刻自动
确定你想在不明确指定接收者的情况下,向多个对象中的一个提交一个
请求可处理一个请求的对相机和应被动太指定。
/// <summary>
/// 职责链模式接口
/// </summary>
public interface IChain
{
IChain Next { get; set; } //下一个职责链
void HandleRequest(string request);
}
/// 职责链模式接口
/// </summary>
public interface IChain
{
IChain Next { get; set; } //下一个职责链
void HandleRequest(string request);
}
/// <summary>
/// 定义一个职责链基类
/// </summary>
public abstract class ChainBase : IChain
{
protected ListBox _Log;
protected IChain _NextChain;
public virtual void HandleRequest(string request) { }
public IChain Next { get { return _NextChain; } set { _NextChain = value; } }
}
/// 定义一个职责链基类
/// </summary>
public abstract class ChainBase : IChain
{
protected ListBox _Log;
protected IChain _NextChain;
public virtual void HandleRequest(string request) { }
public IChain Next { get { return _NextChain; } set { _NextChain = value; } }
}
/// <summary>
/// 刷员工卡上下班或报工
/// </summary>
public class ChainEmployeeCard : ChainBase
{
public ChainEmployeeCard(ListBox log) { _Log = log; }
public override void HandleRequest(string request)
{
string employees = ",E01,E02,E03,"; //模拟员工表的数据
if (employees.IndexOf(request) > 0) //如果员工表存在该卡号,由当前职责链处理请求
{
//在这里处理请求....
_Log.Items.Add(request.ToUpper() ":调用ChainEmployeeCard职责链进行处理!");
}
else if (_NextChain != null)
{
_NextChain.HandleRequest(request);
}
}
}
/// <summary>
/// 刷产品卡进行报工
/// </summary>
public class ChainProductCard : ChainBase
{
public ChainProductCard(ListBox log) { _Log = log; }
public override void HandleRequest(string request)
{
string productCards = ",P01,P02,P03,"; //模拟产品表的数据,每个产品编号对应一个ID卡号
if (productCards.IndexOf(request) > 0)//如果产品表存在该卡号(ID),由当前职责链处理请求
{
//在这里处理请求....
_Log.Items.Add(request.ToUpper() ":调用ChainProductCard职责链进行处理!");
}
else if (_NextChain != null)
{
_NextChain.HandleRequest(request);
}
}
}
/// <summary>
/// 刷批卡进行报工
/// </summary>
public class ChainBatchCard : ChainBase
{
public ChainBatchCard(ListBox log) { _Log = log; }
public override void HandleRequest(string request)
{
//模拟批卡表的数据,批卡是指一组产品的编号,每个批卡编号对应一个ID卡号
string batchCards = ",B01,B02,B03,";
if (batchCards.IndexOf(request) > 0)//如果批卡表存在该卡号(ID),由当前职责链处理请求
{
//在这里处理请求....
_Log.Items.Add(request.ToUpper() ":调用ChainBatchCard职责链进行处理!");
}
else if (_NextChain != null)
{
_NextChain.HandleRequest(request);
}
}
}
/// 刷批卡进行报工
/// </summary>
public class ChainBatchCard : ChainBase
{
public ChainBatchCard(ListBox log) { _Log = log; }
public override void HandleRequest(string request)
{
//模拟批卡表的数据,批卡是指一组产品的编号,每个批卡编号对应一个ID卡号
string batchCards = ",B01,B02,B03,";
if (batchCards.IndexOf(request) > 0)//如果批卡表存在该卡号(ID),由当前职责链处理请求
{
//在这里处理请求....
_Log.Items.Add(request.ToUpper() ":调用ChainBatchCard职责链进行处理!");
}
else if (_NextChain != null)
{
_NextChain.HandleRequest(request);
}
}
}
/// <summary>
/// 处理无法识别的卡号,是职任链的终端
/// </summary>
public class ChainInvalidRequest : ChainBase
{
public ChainInvalidRequest(ListBox log) { _Log = log; }
public override void HandleRequest(string request)
{
//这个职责链是处理终端,再也不会调用下一个职责链处理请求了
//
_Log.Items.Add(request.ToUpper() ":调用ChainInvalidRequest职责链进行处理!");
_Log.Items.Add("请求无法识别!");
}
}
/// <summary>
/// 职责链生成器. 实际是一个标准的生成器模式
/// </summary>
public class ChainBuilder
{
/// <summary>
/// 生成一个职责链. (链头)A->B->C->D->E->F->...n->终端职责(链尾)
/// </summary>
public static IChain BuildChain(ListBox log)
{
//链头,分析是否管理卡号的优先级最高. 链头可以自由更改
IChain header = new ChainCommandCard(log);//链头
IChain chain = header;
//建立职责链处理顺序. 如需求有变在这里调整职责链顺序
chain = chain.Next = new ChainEmployeeCard(log);
chain = chain.Next = new ChainBatchCard(log);
chain = chain.Next = new ChainProductCard(log);
chain = chain.Next = new ChainInvalidRequest(log);
return header;//返回职责链实例
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
主窗体的代码:
public partial class frmMain : Form
{
private IChain _MyChain = null;
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
//调用生成器创建一个完整的职责链
_MyChain = ChainBuilder.BuildChain(this.listBox1);
}
private void btnAnalysis_Click(object sender, EventArgs e)
{
if (txtCardNo.Text.Length < 3)
{
MessageBox.Show("卡号太短啦!");
return;
}
if (txtCardNo.Text.Trim() != "")
{
_MyChain.HandleRequest(txtCardNo.Text.ToUpper().Trim()); //分析请求,匹配职责链....
txtCardNo.Clear();
txtCardNo.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void frmMain_Activated(object sender, EventArgs e)
{
txtCardNo.Focus();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
{
private IChain _MyChain = null;
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
//调用生成器创建一个完整的职责链
_MyChain = ChainBuilder.BuildChain(this.listBox1);
}
private void btnAnalysis_Click(object sender, EventArgs e)
{
if (txtCardNo.Text.Length < 3)
{
MessageBox.Show("卡号太短啦!");
return;
}
if (txtCardNo.Text.Trim() != "")
{
_MyChain.HandleRequest(txtCardNo.Text.ToUpper().Trim()); //分析请求,匹配职责链....
txtCardNo.Clear();
txtCardNo.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
private void frmMain_Activated(object sender, EventArgs e)
{
txtCardNo.Focus();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
请下载附件 (Source for VIP)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网