Chain of Responsibility - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Chain of Responsibility

Description:

Chain of Responsibility. Sheridan Thirsk. Eric Thiessen. Hayder Juhad ... Tarr, Bob. ( 2004). The Chain of Responsibility Pattern - Design Patterns in Java. ... – PowerPoint PPT presentation

Number of Views:587
Avg rating:3.0/5.0
Slides: 22
Provided by: homepages6
Category:

less

Transcript and Presenter's Notes

Title: Chain of Responsibility


1
Chain of Responsibility
  • Sheridan Thirsk
  • Eric Thiessen
  • Hayder Juhad

2
Introduction
  • Behavioural object design pattern
  • Promotes loose coupling
  • Chain of handlers
  • Only one handler can handle request
  • Pass request down the chain
  • Sender does not know handler explicitly

3
Action
  • Client initiates request
  • First object in chain attempts to handle request
  • If it can, it does!
  • If not, it passes the request to its successor
  • Succesor attempts to handle request
  • The pattern continues...

4
Participants
  • Abstract Handler
  • Defines common interface
  • Links to successor
  • Concrete Handler
  • Extends abstract handler
  • Each one can handle certain type of request
  • Only access its successor
  • Client
  • Initiate request
  • Not known which handler will handle request

5
Class Diagram
  • Figure 1 Class Diagram of the Chain of
    Responsibility

6
Applicability
  • Consider using Chain of Responsibility if
  • Multiple possible handlers
  • No receiver specified
  • Dynamic specification of responsibility

7
Benefits
  • Low Coupling
  • Object does not know who handler is
  • Reduces interaction
  • Sender and receiver need not know each other
  • Flexibility
  • Reorder chain
  • Add or remove handlers dynamically

8
Drawbacks
  • Cannot guarantee receipt
  • Request may get passed along by ALL handlers
  • Request falls off the end of the chain

9
Real Example
  • Security Monitoring System
  • Various sensors
  • Hierarchy of information passed up
  • Passed up higher until object handles it
  • Domain Name Server (DNS)
  • Client connect to local DNS
  • Server passes request until it is handled

10
Known Uses
  • Windows system to handle input
  • GUI events
  • From object where event occurred to Handler
  • Pass events up container hierarchy
  • Known as
  • Responder in Java 1.0 AWT
  • Event-Handler in MacApp
  • Bureaucrat in Symantecs TCL library

11
Sample Code
  • //Base Class
  • class Handler
  • protected
  • Handler successor
  • public
  • void SetSuccessor(Handler s)
  • virtual void HandleRequest(void r)

12
Sample Code
  • //Domain Name Server
  • class DNS public Handler
  • private
  • Char ServerName
  • DomainNameEntry DNSTable
  • int size
  • public
  • // Adds a entry in the DNSTable
  • void Add(char DomainName, char b1, b2, b3,
    b4)
  • void HandleRequest(char request)
  • // if request in DNSTable then return entry
  • // otherwise pass request to successor if
    // it exists

13
Sample Code
  • void main()
  • // create servers
  • DNS A("Server A")
  • DNS B("Server B")
  • DNS C("Server C")
  • // setup a Chain of Responsibility
  • A.SetSuccessor(B)
  • B.SetSuccessor(C)
  • C.SetSuccessor(NULL)
  • //initialize the tables for the servers
  • A.Add("google.com",124,32,4,1)
  • A.Add("amazon.com",224,132,14,6)
  • ...
  • C.Add("i-like-puppies.com",12,23,4,114)

14
Sample Code
  • //get user request
  • char input255
  • cout ltlt"Input Request "
  • cin gtgt input
  • //pass request to server A
  • A.HandleRequest(input)

15
Related Patterns
  • Composite
  • Parent is successor to child
  • Parent asks all children to perform operation
  • Visitor
  • Visitor Handle Request
  • Visited cannot pass the request
  • Iterator
  • Same operation
  • Structure is different
  • Iterator Sequential chain
  • CoR Many possibilities. Linked list, tree, etc.

16
Conclusion
  • Its just a chain!
  • Each object in chain is potential handler
  • Link to next object, successor
  • Unknown handler
  • Pros
  • Reduced coupling
  • Increased flexibility
  • Con
  • Can fall off the end
  • Decouple requester and handler
  • Especially with multiple handlers

17
Any Questions?
  • Well, we have two!
  • And we have answers

18
QA 1
  • What can be done about requests falling off the
    end of the chain?!?
  • Use a Controller with error handling
  • Use exception handling

19
QA 2
  • What about an circular chain?!?
  • Keep track of chain length
  • Count times the request is passed
  • If greater than length going in circles
  • Error handling, throw an exception, etc.

20
Thank you!
  • Check out our website
  • http//homepages.ucalgary.ca/sjthirsk/
  • Contact us with any additional questions
  • Eric
  • ethiessen_at_gmail.com
  • Sheridan
  • sheridan_at_shaw.ca
  • Hayder
  • hjuhad_at_ucalgary.ca
  • Or ask us in person!

21
References
  • Cunningham. (2004). Chain of Responsibility
    Pattern - Cunningham Cunningham, Inc. July 14,
    2004.http//c2.com/cgi/wiki?ChainOfResponsibility
    Pattern
  • (Gamma, Helm, Johnson Vlissides, 1995) E.
    Gamma, R.Helm, R.Johnson, J.Vlissides (1995).
    Design Patterns. Addison Wesley. ISBN
    0.201-63361-2.
  • Roe, Cameron. (1998). Chain of Responsibility
    Design Pattern - SENG 609.04 Winter 1998.
    Software Engineering Research Network, University
    of Calgary. March 22, 1998.http//sern.ucalgary.c
    a/courses/SENG/609.04/W98/roec/Chain/chain.htmlDi
    sscussion
  • Sarma, T. Kulathu. (2001). Chain of
    Responsibility - The Code Project. July 11,
    2001.http//www.codeproject.com/gen/design/chain_
    response.asp
  • Tarr, Bob. (2004). The Chain of Responsibility
    Pattern - Design Patterns in Java. April 15,
    2004.http//www.research.umbc.edu/tarr/dp/lectur
    es/Chain.pdf
  • To, Ha. (1999). Chain of Responsibility -
    Department of Computer Engineering, Santa Clara
    University. February 19, 1999.http//sol.students
    .engr.scu.edu/hto1/pattern/ddcor.html
Write a Comment
User Comments (0)
About PowerShow.com