C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

模式设计c#--行为型--Chain of Responsibility

名称 Chain of Responsibility
结构 o_china of responsilility.bmp
意图 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
适用性
  • 有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
  • 你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
  • 可处理一个请求的对象集合应被动态指定。

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

    }

}


另一个很好的例子:
/***************************************************** 
* 一个责任链模式例子 
*该例子是三类售货员处理订单的情况 
*如果订单金额小于1000,则一级售货员可以处理该订单 
*如果订单金额小于10000,则二级售货员可以处理该订单 
*如果订单金额小于100000,则三级售货员可以处理该订单 
*****************************************************
*/
 
 
using System; 
 
/**//// <summary> 
///售货员接口,所有类型的售货员必须实现该接口 
/// </summary> 

interface ISalesMan 

    
string Name {set;get;} //售货员名字 
    void SetNext(ISalesMan nextSalesMan); //设置下一级售货员 
    void Process(Order order); //处理订单 
}
 
 
/**//// <summary> 
/// 订单类 
/// </summary> 

class Order 

    
private int orderAmount; 
 
    
public int Amount 
    

        
setthis.orderAmount = value;} 
        
getreturn this.orderAmount; } 
    }
 
}
 
 
/**//// <summary> 
/// 一类售货员 
/// </summary> 

class FirstSalesMan : ISalesMan 

    
private ISalesMan nextSalesMan = null
    
private string name = string.Empty; 
 
    ISalesMan 成员
ISalesMan 成员 
}
 
 
/**//// <summary> 
/// 二类售货员 
/// </summary> 

class SecondSalesMan : ISalesMan 

    
private ISalesMan nextSalesMan = null
    
private string name = string.Empty; 
 
    ISalesMan 成员
ISalesMan 成员 
}
 
 
/**//// <summary> 
/// 三类售货员 
/// </summary> 

class ThirdSalesMan : ISalesMan 

    
private ISalesMan nextSalesMan = null
    
private string name = string.Empty; 
 
    ISalesMan 成员
ISalesMan 成员 
}
 
 
class Client 

    
public static void Main(string[] args) 
    

        FirstSalesMan first 
= new FirstSalesMan(); 
        first.Name 
= "firstMan"
 
        SecondSalesMan second 
= new SecondSalesMan(); 
        second.Name 
= "secondMan"
 
        ThirdSalesMan third 
= new ThirdSalesMan(); 
        third.Name 
= "thirdMan"
 
        first.SetNext(second); 
        second.SetNext(third); 
 
        Order o 
= new Order(); 
        o.Amount 
= 300
        first.Process(o); 
 
        o 
= new Order(); 
        o.Amount 
= 1300
        first.Process(o); 
 
        o 
= new Order(); 
        o.Amount 
= 11300
        first.Process(o); 
 
        Console.Read(); 
    }
 
}
 

posted on 2006-01-03 16:06 梦在天涯 阅读(741) 评论(1)  编辑 收藏 引用 所属分类: Design pattern

评论

# re: 模式设计c#--行为型--Chain of Responsibility 2006-04-24 16:47 梦在天涯

职责链(Chain of Responsibility)模式
责任链模式是一种对象的行为模式【GOF95】。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织链和分配责任。

  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1790694
  • 排名 - 5

最新评论

阅读排行榜