Patterns in SOME –Chain Of Responsibility

来源:互联网 发布:武汉java培训机构名单 编辑:程序博客网 时间:2024/05/01 21:51
Code in C#:
 
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;
         }
     }
}
 
 
Code in SOME:
  
AHandler ->AHandler[m_successorHandler]
       a_HandleCRequest(CRequest )
       SetSuccessor(AHandler m_successorHandler)
 
CConcreteHandler1 : AHandler
       o_HandleCRequest(CRequest)
 
CConcreteHandler2 : AHandler
       o_HandleCRequest(CRequest)
 
CConcreteHandler3 : AHandler
       o_HandleCRequest(CRequest)
 
CRequest
       (int _iCRequestType, string _strCRequestParameters)        //include defination of 2 private members
 
CClient
       main
 
 
CClient.main
{
       // Set up chain (usually one need to be done once)
       AHandler firstHandler<CConcreteHandler1>.();
       AHandler secondHandler<CConcreteHandler2>.();
       AHandler thirdHandler<CConcreteHandler3>.();
      
       firstHandler.SetSuccessor(m_successorHandler = secondHandler);
       secondHandler.SetSuccessor(m_successorHandler = thirdHandler);
 
       // generate and fire CRequest
       CRequest newCRequest.(2,"This are the CRequest parameters");
       firstHandler.HandleCRequest<CConcreteHandler1>(CRequest[newCRequest])                       //alias of newCRequest
       {
              <%
                     // determine if we can handle the CRequest
                     if (CRequest.CRequestType == 1) // some complex decision making!
                     {
                            // CRequest handling code goes here
                            Console.WriteLine("CRequest handled in ConcreteHandler1");
                     }
                     else
                     {
                            // not handled here - pass on to next in the chain
                            if (m_successorHandler != null)
                     %>
                                   m_successorHandler.HandleCRequest<CConcreteHandler2>(CRequest)
                                   {
                                          <%
                                                 // determine if we can handle the CRequest
                                                 if (CRequest.CRequestType == 2) // some complex decision making!
                                                 {
                                                        // CRequest handling code goes here
                                                        Console.WriteLine("CRequest handled in ConcreteHandler2");
                                                 }
                                                 else
                                                 {
                                                        // not handled here - pass on to next in the chain
                                                        if (m_successorHandler != null)
                                                 %>
                                                               m_successorHandler.HandleCRequest<CConcreteHandler3>(CRequest)
                                                               {
                                                                      <%
                                                                             // determine if we can handle the CRequest
                                                                             if (CRequest.CRequestType == 3) // some complex decision making!
                                                                             {
                                                                                    // CRequest handling code goes here
                                                                                    Console.WriteLine("CRequest handled in ConcreteHandler3");
                                                                             }
                                                                             else
                                                                             {
                                                                                    // not handled here - pass on to next in the chain
                                                                                    if (m_successorHandler != null)
                                                                                           m_successorHandler.HandleCRequest(CRequest);
                                                                             }           
                                                                      %>
                                                               };
                                                 <%}%>
                                   };
              <%}%>
       };
}
原创粉丝点击