跳到主要內容

職責鏈模式(Chain of Responsibility)


  • 使多個物件都有機會處理請求,從而避免的發送者和接收者之間的耦合關係。將這個物件連成一條鏈,並沿著這條鏈傳遞該請求,直到有一個物件處理它為止。
基本架構圖


實例架構圖
基本程式碼
using System;

namespace DPExample
{
    abstract class Handler
    {
        protected Handler successor;

        public void SetSuccessor(Handler successor)
        {
            this.successor = successor;
        }

        public abstract void HandleRequest (int request);
    }

    class ConcreteHandler1 : Handler
    {
        public override void HandleRequest (int request)
        {
            if (request >= 0 && request < 10) {
                Console.WriteLine ("{0} 處理請求 {1}", this.GetType ().Name, request);
            } else if (successor != null) {
                successor.HandleRequest(request);
            }
        }
    }
    
    class ConcreteHandler2 : Handler
    {
        public override void HandleRequest (int request)
        {
            if (request >= 10 && request < 20) {
                Console.WriteLine ("{0} 處理請求 {1}", this.GetType ().Name, request);
            } else if (successor != null) {
                successor.HandleRequest(request);
            }
        }
    }

    
    class ConcreteHandler3 : Handler
    {
        public override void HandleRequest (int request)
        {
            if (request >= 20 && request < 30) {
                Console.WriteLine ("{0} 處理請求 {1}", this.GetType ().Name, request);
            } else if (successor != null) {
                successor.HandleRequest(request);
            }
        }
    }
}
using System;

namespace DPExample
{
 public class Program
 {
  static void Main ()
        {
            Handler h1 = new ConcreteHandler1 ();
            Handler h2 = new ConcreteHandler2 ();
            Handler h3 = new ConcreteHandler3 ();
            h1.SetSuccessor (h2);
            h2.SetSuccessor (h3);

            int[] requests = {2,5,14,22,18,3,27,20};

            foreach (int request in requests) {
                h1.HandleRequest(request);
            }
  }
 }
}
實例程式碼
using System;

namespace DPExample
{
    class 申請
    {
        private string requestType;
        public string 申請類別 {
            get { return requestType; }
            set { requestType = value; }
        }

        private string requestContent;
        public string 申請內容 {
            get { return requestContent; }
            set { requestContent = value; }
        }

        private int number;
        public int 數量 {
            get { return number; }
            set { number = value; }
        }
    }

    abstract class 管理者
    {
        protected string name;
        protected 管理者 superior;

        public 管理者(string name)
        {
            this.name = name;
        }

        public void 設定上級管理者(管理者 superior)
        {
            this.superior = superior;
        }

        abstract public void 處理請求(申請 request);
    }

    class 經理 : 管理者
    {
        public 經理 (string name) : base(name)
        {
        }

        public override void 處理請求 (申請 request)
        {
            if (request.申請類別 == "請假" && request.數量 <= 2)
                Console.WriteLine ("{0}:{1} 數量{2} 被批准", name, request.申請內容, request.數量);
            else {
                if (superior != null)
                    superior.處理請求(request);
            }
        }
    }

    class 總監 : 管理者
    {
        public 總監 (string name) : base(name)
        {
        }

        public override void 處理請求 (申請 request)
        {
            if (request.申請類別 == "請假" && request.數量 <= 5)
                Console.WriteLine ("{0}:{1} 數量{2} 被批准", name, request.申請內容, request.數量);
            else {
                if (superior != null)
                    superior.處理請求(request);
            }
        }
    }

    class 總經理 : 管理者
    {
        public 總經理 (string name) : base(name)
        {
        }

        public override void 處理請求 (申請 request)
        {
            if (request.申請類別 == "請假")
                Console.WriteLine ("{0}:{1} 數量{2} 被批准", name, request.申請內容, request.數量);
            else if (request.申請類別 == "加薪" && request.數量 <= 500)
                Console.WriteLine("{0}:{1} 數量{2} 被批准", name, request.申請內容, request.數量);
            else if (request.申請類別 == "加薪" && request.數量 > 500)
                Console.WriteLine("{0}:{1} 數量{2} 再說吧", name, request.申請內容, request.數量);
        }
    }
}
using System;


namespace DPExample
{
 public class Program
 {
  static void Main ()
        {
            經理 jinli = new 經理("金利");
            總監 zongjian = new 總監("宗劍");
            總經理 zhongjingli = new 總經理("鐘精勵");
            jinli.設定上級管理者(zongjian);
            zongjian.設定上級管理者(zhongjingli);

            申請 request = new 申請();
            request.申請類別 = "請假";
            request.申請內容 = "小菜請假";
            request.數量 = 1;
            jinli.處理請求(request);

            申請 request2 = new 申請();
            request2.申請類別 = "請假";
            request2.申請內容 = "小菜請假";
            request2.數量 = 4;
            jinli.處理請求(request2);

            申請 request3 = new 申請();
            request3.申請類別 = "加薪";
            request3.申請內容 = "小菜請求加薪";
            request3.數量 = 500;
            jinli.處理請求(request3);

            申請 request4 = new 申請();
            request4.申請類別 = "加薪";
            request4.申請內容 = "小菜請求加薪";
            request4.數量 = 1000;
            jinli.處理請求(request4);

  }

 }
}

留言

這個網誌中的熱門文章

用 C# 批次控制 Word 合併列印

前由 我有全區的電話資料,問題在於我要依不同里別來製作出電話簿。結果如下圖: 單純採用合併列印無法達成我的需求。解決方法係用「功能變數」儲存上一個里別,與現在里別進行比較:若不同,則換頁。不過,這樣功能變數還蠻長的。最後,我還是採用 C# 來解決。 解決方案 用 C# 控制 WORD 中合併列印的「資料來源 Data Source」,給予不同里別的「sqlstatement」。迴圈處理不同的里別即可。但可預見其處理過程會很慢,不過還好,我可以不用在意它,有跑出結果即可。 程式碼 IList<string> areas = new List<string>() { "後壁", "侯伯", "嘉苳", "土溝", "嘉田", "嘉民", "菁豊", "崁頂", "後廍", "墨林", "菁寮", "新嘉", "頂長", "平安", "仕安", "竹新", "新東", "長安", "頂安", "福安", "烏樹" }; string root = @"D:\"; // 根目錄 string data = root + @"\data.docm"; // 資料檔(即資料來源) string template = root + @"\template.docx"; // 已設定好格式與合併欄位的 Word 檔 string output = @"d:\Final"; // 輸出之資料夾 object oMissing = System.Reflection.Missing.Va...

VLC c# 順利編譯

原文網址: http://www.cnblogs.com/haibindev/archive/2011/12/21/2296173.html 原文作者: haibindev 原文標題:c#万能视频播放器 本文的重點在於修正 class VlcPlayer,使其能順利在 VC# Express 2010 .Net Framework 4 下順利編譯。 修正重點在於 CallingConvention = CallingConvention. StdCall 改成 CallingConvention = CallingConvention. Cdecl using System; using System.Runtime.InteropServices; using System.Security; using System.Text; namespace VlcDotNet { class VlcPlayer { private IntPtr libvlc_instance_; private IntPtr libvlc_media_player_; private double duration_; public VlcPlayer(string pluginPath) { string plugin_arg = "--plugin-path=" + pluginPath; string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg }; libvlc_instance_ = LibVlcAPI.libvlc_new(arguments); libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_); } public ...

[Symfony+Doctrine] 透過非 Id 來使用 Pessimistic Lock

根據 文件 ,Doctrine 有 Pessimistic Lock,又分為兩種: LockMode::PESSIMISTIC_WRITE:對應至 MySQL 的 Select FOR UPDATE LockMode::PESSIMISTIC_READ:對應至 MySQL 的 Select LOCK IN SHARE MODE 差別在於 LOCK IN SHARE MODE 會將在 row 的資料鎖定(row-level lock),在非同一交易(Transaction)下,不給寫入,其他交易可以讀取, 且可以繼續 Select LOCK IN SHARE MODE 。而 FOR UPDATE 不僅鎖定該資料,在非同一交易下,不給寫入,其它交易可以讀取, 但不能 Select LOCK IN SHARE MODE 。MySQL 文件有更詳細的比較與情境使用的說明,參考 網址 。 現在問題是,我們要完全採用 ORM 來處理資料。Doctrine 的文件提到 EntityManager::find 可以採用 Pessimistic Lock, 但 find 是透過 id 來處理 。而其他 find 系列函數(包括:findAll, findBy, findOneBy)皆不支援 LockMode。 因此,勢必要有方法來「透過非 id 來使用 Pessimistic Lock」。透過查看原始碼,簡單的方法是有的,解法之範例如下: 19 public function depositAction() 20 { 21 22 $em = $this->getDoctrine()->getManager(); 23 24 $em->transactional(function ($em) { 25 $entityName = 'AcmeTrainingBundle:Account'; 26 $lockMode = LockMode::PESSIMISTIC_READ; 27 $orderBy = null; 28 $...