AbstractExecutor
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.ChainOfResponsibility
{
/**//// <summary>
/// 抽象處理者(Handler)角色
/// </summary>
public abstract class AbstractExecutor
{
/**//// <summary>
/// 抽象處理者(Handler)角色
/// </summary>
protected AbstractExecutor _executor;
/**//// <summary>
/// 設置責任鏈的上一級對象
/// </summary>
/// <param name="executor"></param>
public void SetSuccessor(AbstractExecutor executor)
{
this._executor = executor;
}
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns>執行者;內容;時間</returns>
public abstract string Insert(MessageModel mm);
}
}
Employee
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.ChainOfResponsibility
{
/**//// <summary>
/// 具體處理者(ConcreteHandler)角色
/// </summary>
public class Employee : AbstractExecutor
{
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns>執行者;內容;時間</returns>
public override string Insert(MessageModel mm)
{
string rtn = "";
// 插入的信息字符數小於5
if (mm.Message.Length < 5)
{
SqlMessage m = new SqlMessage();
if (m.Insert(mm))
{
rtn = "執行者:雇員" + " 內容:" + mm.Message + " 時間:" + mm.PublishTime.ToString();
}
}
// 否則讓上級去執行
else if (base._executor != null)
{
rtn = _executor.Insert(mm);
}
return rtn;
}
}
}