程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 設計模式(C#) - 責任鏈模式(Chain of Responsibility Pattern)

設計模式(C#) - 責任鏈模式(Chain of Responsibility Pattern)

編輯:關於C#

返回“設計模式(C#)系列文章索引”

介紹

為解除請求的發送者和接收者之間耦合,而使多個對象都有機會處理這個請求。將這些對象連成一條鏈,並沿著這條鏈傳遞該請求,直到有一個對象處理它。

示例

有一個Message實體類,某個類對它的操作有Insert()方法。現在要求根據插入信息的字符長度,讓不同的對象去處理,這些對象連成一條鏈,並沿著這條鏈傳遞該請求,直到有一個對象處理它。

MessageModel

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.ChainOfResponsibility
{
  /**//// <summary>
  /// Message實體類
  /// </summary>
  public class MessageModel
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="msg">Message內容</param>
    /// <param name="pt">Message發布時間</param>
    public MessageModel(string msg, DateTime pt)
    {
      this._message = msg;
      this._publishTime = pt;
    }

    private string _message;
    /**//// <summary>
    /// Message內容
    /// </summary>
    public string Message
    {
      get { return _message; }
      set { _message = value; }
    }

    private DateTime _publishTime;
    /**//// <summary>
    /// Message發布時間
    /// </summary>
    public DateTime PublishTime
    {
      get { return _publishTime; }
      set { _publishTime = value; }
    }
  }
}

SqlMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.ChainOfResponsibility
{
  /**//// <summary>
  /// Sql方式操作Message
  /// </summary>
  public class SqlMessage
  {
    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public bool Insert(MessageModel mm)
    {
      // 代碼略
      return true;
    }
  }
}

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;
    }
  }
}

Leader

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.ChainOfResponsibility
{
  /**//// <summary>
  /// 抽象處理者(Handler)角色
  /// </summary>
  public class Leader : AbstractExecutor
  {
    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    /// <returns>執行者;內容;時間</returns>
    public override string Insert(MessageModel mm)
    {
      string rtn = "";

      // 插入的信息字符數小於10
      if (mm.Message.Length < 10)
      {
        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;
    }
  }
}

Manager

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.ChainOfResponsibility
{
  /**//// <summary>
  /// 抽象處理者(Handler)角色
  /// </summary>
  public class Manager : AbstractExecutor
  {
    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    /// <returns>執行者;內容;時間</returns>
    public override string Insert(MessageModel mm)
    {
      string rtn = "";

      // 插入的信息字符數小於15
      if (mm.Message.Length < 15)
      {
        SqlMessage m = new SqlMessage();

        if (m.Insert(mm))
        {
          rtn = "執行者:經理" + " 內容:" + mm.Message + " 時間:" + mm.PublishTime.ToString();
        }
      }
      else
      {
        rtn = "你所插入的Message不符合要求";
      }

      return rtn;
    }
  }
}

client

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Pattern.ChainOfResponsibility;

public partial class ChainOfResponsibility : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    AbstractExecutor employee = new Employee();
    AbstractExecutor leader = new Leader();
    AbstractExecutor manager = new Manager();
    employee.SetSuccessor(leader);
    leader.SetSuccessor(manager);

    Response.Write(employee.Insert(new MessageModel("abcd", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(employee.Insert(new MessageModel("abcdefgh", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(employee.Insert(new MessageModel("abcdefghigkl", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(employee.Insert(new MessageModel("abcdefghigklmnop", DateTime.Now)));
  }
}

運行結果

執行者:雇員 內容:abcd 時間:2007-4-22 20:04:41

執行者:主管 內容:abcdefgh 時間:2007-4-22 20:04:41

執行者:經理 內容:abcdefghigkl 時間:2007-4-22 20:04:41

你所插入的Message不符合要求

參考

http://www.dofactory.com/Patterns/PatternChain.aspx

OK

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved