<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    一、 職責鏈(Chain of Responsibility)模式

    責任鏈模式是一種對象的行為模式【GOF95】。在責任鏈模式里,很多對象由每一個對象對其下家的引用而連接起來形成一條鏈。請求在這個鏈上傳遞, 直到鏈上的某一個對象決定處理此請求。發出這個請求的客戶端并不知道鏈上的哪一個對象最終處理這個請求,這使得系統可以在不影響客戶端的情況下動態地重新 組織鏈和分配責任。

    從擊鼓傳花談起

    擊鼓傳花是一種熱鬧而又緊張的飲酒游戲。在酒宴上賓客依次坐定位置,由一人擊鼓,擊鼓的地方與傳花的地方是分開的,以示公正。開始擊鼓時,花束就開始依次傳遞,鼓聲一落,如果花束在某人手中,則該人就得飲酒。

    擊鼓傳花便是責任鏈模式的應用。責任鏈可能是一條直線、一個環鏈或者一個樹結構的一部分。


    二、 責任鏈模式的結構

    責任鏈模式涉及到的角色如下所示:

    抽象處理者(Handler)角色:定義出一個處理請求的接口。如果需要,接口可以定義出一個方法,以設定和返回對下家的引用。這個角色通常由一個抽象類或接口實現。

    具體處理者(ConcreteHandler)角色:具體處理者接到請求后,可以選擇將請求處理掉,或者將請求傳給下家。由于具體處理者持有對下家的引用,因此,如果需要,具體處理者可以訪問下家。



    Chain of Responsibility模式的實現
    要實現Chain of Responsibility模式,需要滿足該模式的基本要件:
    1,對象鏈的組織。需要將某任務的所有職責執行對象以鏈的形式加以組織。
    2,消息或請求的傳遞。將消息或請求沿著對象鏈傳遞,以讓處于對象鏈中的對象得到處理機會。
    3,處于對象鏈中的對象的職責分配。不同的對象完成不同的職責。
    4,任務的完成。處于對象鏈的末尾的對象結束任務并停止消息或請求的繼續傳遞。

    Chain of Responsibility模式的優優缺點:
    優點:
    1,責任的分擔。每個類只需要處理自己該處理的工作(不該處理的傳遞給下一個對象完成),明確各類的責任范圍,符合類的最小封裝原則。
    2,可以根據需要自由組合工作流程。如工作流程發生變化,可以通過重新分配對象鏈便可適應新的工作流程。
    3,類與類之間可以以松耦合的形式加以組織。
    缺點:
    因為處理時以鏈的形式在對象間傳遞消息,根據實現方式不同,有可能會影響處理的速度。



    例子:
    namespace ChainOfResponsibility_DesignPattern
    {
        
    using System;

        
    abstract class Handler 
        {
            
    protected Handler successorHandler;
            
    abstract public void HandleRequest(Request request);        
            
    public void SetSuccessor(Handler sucessor)
            {
                successorHandler 
    = sucessor;
            }
        }

        
    class ConcreteHandler1 : Handler
        {
            
    override public void HandleRequest(Request request)
            {
                
    // determine if we can handle the request
                if (request.RequestType == 1// some complex decision making!
                {
                    
    // request handling code goes here
                    Console.WriteLine("request handled in ConcreteHandler1");
                }
                
    else 
                {
                    
    // not handled here - pass on to next in the chain
                    if (successorHandler != null)
                        successorHandler.HandleRequest(request);
                }
            }
        }

        
    class ConcreteHandler2 : Handler
        {
            
    override public void HandleRequest(Request request)
            {
                
    // determine if we can handle the request
                if (request.RequestType == 2// some complex decision making!
                {
                    
    // request handling code goes here
                    Console.WriteLine("request handled in ConcreteHandler2");
                }
                
    else 
                {
                    
    // not handled here - pass on to next in the chain
                    if (successorHandler != null)
                        successorHandler.HandleRequest(request);
                }
            }
        }

        
    class ConcreteHandler3 : Handler
        {
            
    override public void HandleRequest(Request request)
            {
                
    // determine if we can handle the request
                if (request.RequestType == 3// some complex decision making!
                {
                    
    // request handling code goes here
                    Console.WriteLine("request handled in ConcreteHandler3");
                }
                
    else 
                {
                    
    // not handled here - pass on to next in the chain
                    if (successorHandler != null)
                        successorHandler.HandleRequest(request);
                }        
            }
        }

        
    class Request 
        {
            
    private int iRequestType;
            
    private string strRequestParameters;

            
    public Request(int requestType, string requestParameters)
            {
                iRequestType 
    = requestType;    
                strRequestParameters 
    = requestParameters;
            }

            
    public int RequestType 
            {
                
    get 
                {
                    
    return iRequestType;
                }
                
    set 
                {
                    iRequestType 
    = value;
                }
            }
        }

        
    /// <summary>
        
    ///    Summary description for Client.
        
    /// </summary>
        public class Client
        {
            
    public static int Main(string[] args)
            {
                
    // Set up chain (usually one need to be done once)
                Handler firstHandler = new ConcreteHandler1();
                Handler secondHandler 
    = new ConcreteHandler2();
                Handler thirdHandler 
    = new ConcreteHandler3();
                firstHandler.SetSuccessor(secondHandler);
                secondHandler.SetSuccessor(thirdHandler);

                
    // After setting up the chain of responsibility, we can
                
    // now generate requests and pass then off to the 
                
    // chain to be handled

                
    // generate and fire request
                Request newRequest = new Request(2,"This are the request parameters");
                firstHandler.HandleRequest(newRequest);
                
                
    return 0;
            }
        }
    }


    posted on 2009-12-23 15:09 seal 閱讀(408) 評論(0)  編輯  收藏 所屬分類: 設計模式
    主站蜘蛛池模板: 永久黄网站色视频免费直播| 免费观看在线禁片| 成年女人男人免费视频播放| 亚洲高清在线观看| 国产高清不卡免费视频| 亚洲久本草在线中文字幕| 四虎成人精品永久免费AV| 777亚洲精品乱码久久久久久 | 久久久久成人精品免费播放动漫| 国产亚洲精品不卡在线| 一级有奶水毛片免费看| 亚洲精品狼友在线播放| 国产精品免费一区二区三区四区 | 亚洲综合图片小说区热久久| 最近中文字幕免费2019| 亚洲人成影院午夜网站| 好爽…又高潮了免费毛片| 亚洲AV成人精品日韩一区| 国产精品久久免费视频| jizz18免费视频| 亚洲大成色www永久网站| 2019中文字幕在线电影免费 | 亚洲七七久久精品中文国产| 九九热久久免费视频| 亚洲无删减国产精品一区| 日韩视频在线精品视频免费观看| 亚洲综合久久一本伊伊区| 国产免费观看a大片的网站| 国产精品免费αv视频| 亚洲嫩模在线观看| 女人被免费视频网站| www免费黄色网| 亚洲理论精品午夜电影| 又大又黄又粗又爽的免费视频| 久久毛片免费看一区二区三区| 亚洲第一二三四区| 亚洲乱码中文字幕手机在线 | 100000免费啪啪18免进| 黄色免费网址大全| 337p日本欧洲亚洲大胆精品555588| 成人男女网18免费视频|