Click here to Skip to main content
Click here to Skip to main content
Go to top

Chain of Responsibility

, 29 Jul 2006
Rate this:
Please Sign up or sign in to vote.
Chain of Responsibility
Behavioural Pattern Chain of Responsibility

Introduction

Behavioural patterns are concerned with assigment of responsibilities between objects. But they have complex control flows that are difficult to follow at runtime.The objects are interconnected such that they do not even allow us to eventually follow the control.

It is not always not possible that single objects can process all requests, they always transfer requests to other objects at runtime to get their work done, by maintaining references of these objects. Too much understanding of objects by one another have the great chance of increasing coupling, the design should ensure that they should be maintained minimal.

Background (Chain of Responsibility) - 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.

Reference

  • Design Patterns, Elements of Reusable object oriented software by GOF

Some More Information

The sender sends the request and the receiver accepts the same and acts accordingly. The receiver of the request is more than one in number arranged in chain which are capable of either receiving the request, or forwarding the request to the similar object. The sender who makes the request is not aware of the object handling it, but sure that atleast one object (default) will answer its call. Every receiving object handles the same interface with different implementation for handling the request. Also each object in the chain should have the reference to the neighbouring objects in the chain to forward the request if they are not capable for handling the same.

The Chain of Resposibility will not really fit in a design if there exists a scenario where there is a possibility that none of the objects can respond to the sender such that the request can go unhandled.

Perfect Example

A school headmaster (Client) arranged a teacher (Handler) for improving the general knowledge of the students. So he picked up ten students randomly from a class and hired a teacher. The teacher took the class for a week. The head master wanted to test the credibility of the teacher so suddenly visits the class and asked the teacher to question (request) the student in front of him.

The teacher always picked up a single student and asked him a question. He answered if he knew, else he selected any one among nine. It goes on like this until the teacher's question was answered. If all the first nine students do not answer, the tenth guy answers, also the teacher was very well aware that this tenth guy was an intelligent guy (Default handler) who can answer all her questions if none answered. The head master who made the request to the teacher had no knowledge of who can answer the teachers question. So the request has an implicit receiver.

UML

UML

Chain of responsibility will not fit for unhandled requests. Say now the teacher is aware that nobody can answer her, what would be the point to ask the question.

Participants

Handler (Teacher)

Defines the interface for handling the request (HandleQuestions).
Implements the successor link. The student needs to know to whom the question is to be passed if he does not know the answer.

Concrete Handler (PoorStudent, AverageStudent, ExcellentStudent)

Handles the request it is responsible for (poor student answers easy questions, that is his responsibility). Can access the successor.
If the poor student cannot answer the question, he passes the same to the next student.

Client (Head Master)

Initiates the request to object on chain.

Source Code (Client)

using System;
using System.Collections.Generic;
using System.Text;
using dptrainer.Behavioural_Pattern.Chain_of_Responsibility;

namespace dptrainer
{
    class Program
    {
        static void Main(string[] args)
        {
            Demo1();
            Console.ReadLine();            
        }

        private static void Demo1()
        {
            Teacher poorstudent = new PoorStudent();
            Teacher averagestudent = new AverageStudent();
            Teacher excellentstudent = new ExcellentStudent();
            poorstudent.PassQuestion(averagestudent);
            averagestudent.PassQuestion(excellentstudent);
            int[] questiontype = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };

            foreach (int question in questiontype)
            {
                poorstudent.HandleQuestions((QuestionType)question);
            }
        }
     }
}

History

  • Version 1.0 posted on 29 July 2006

Thinking in Patterns can evolve solid solution !

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Subhash Balachandran
Web Developer
Singapore Singapore
"Just began to think in patterns" - Zuboss

Comments and Discussions

 
GeneralMy vote of 1 PinmemberAmol Rawke Pune10-Mar-11 0:55 
GeneralGeneric CoR implementation. PinmemberMember 160075526-Aug-08 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 29 Jul 2006
Article Copyright 2006 by Subhash Balachandran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid