• русский (Россия)
    • English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
  • Вход
  • Зарегистрироваться
DotNetAge - Mvc & jQuery CMS
Скрыть боковую панель

Chain of Responsibility


    • Подаваемым на основании:
    • Behavioral Patterns
  • 0

Definition



Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

UML class diagram





Participants



The classes and/or objects participating in this pattern are:
  • Handler
defines an interface for handling the requests

(optional) implements the successor link

  • ConcreteHandler
handles requests it is responsible for

can access its successor

if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor

  • Client - initiates the request to a ConcreteHandler object on the chain


Sample code in C#




///
/// MainApp startup class for Structural
/// Chain of Responsibility Design Pattern.
///
class MainApp
{
///
/// Entry point into console application.
///
static void Main()
{
// Setup Chain of Responsibility
Handler h1 = new ConcreteHandler1();
Handler h2 = new ConcreteHandler2();
Handler h3 = new ConcreteHandler3();
h1.SetSuccessor(h2);
h2.SetSuccessor(h3);

// Generate and process request
int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };

foreach (int request in requests)
{
h1.HandleRequest(request);
}

// Wait for user
Console.ReadKey();
}
}

///
/// The 'Handler' abstract class
///
abstract class Handler
{
protected Handler successor;

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

public abstract void HandleRequest(int request);
}

///
/// The 'ConcreteHandler1' class
///
class ConcreteHandler1 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 0 && request < 10)
{
Console.WriteLine("{0} handled request {1}",
this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request);
}
}
}

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

///
/// The 'ConcreteHandler3' class
///
class ConcreteHandler3 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 20 && request < 30)
{
Console.WriteLine("{0} handled request {1}",
this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request);
}
}
}

 


    Average:
  • Читает
    (763)
  • Постоянная ссылка
Доля в:

Tag cloud

Anything in here will be replaced on browsers that support the canvas element