AlgoViz

Chain of responsibility

medium 45 min

A request travels past several handlers until one takes it, or until one stops it.

Watch lesson4:36

UML design

Classes, the link, and the path of five tickets.

The point: one abstract class holds the walking, four small classes hold the rules, and one function holds the order.

Classes

The support desk

Support desk

firstTicketApi+ submit(text)«abstract»SupportHandler- next: SupportHandler+ setNext(h)+ handle(t): String# canHandle(t): bool# resolve(t): StringTicket+ id: int+ text: String+ priority: Priority

1/4Entry

The ticket interface knows one handler and calls handle.

usesdepends onextends (is a)

The link to the next handler is the field next, drawn as a row. The next one is not owned: a handler does not create or destroy its successor.

Reading the diagram

  1. 1

    Interface

    setNext links, handle processes or passes. All handlers look alike, so any order can be built.

  2. 2

    Base class

    Holds next and the default forwarding. Without it every team would repeat three lines, and one would forget the null check.

  3. 3

    Concrete handlers

    Each class holds one rule and its own action. A change of the medium rule touches Level2 only.

  4. 4

    Request

    The ticket is data and does not change on its way. Handlers that write into a shared request depend on each other in secret.

  5. 5

    Linkage

    The list ends where next is empty. Termination is either a taker or the end.

Over time

Two journeys

LOW stops early, UNKNOWN goes to the end

The reply of Triage really returns through Engineering, Level2 and Level1. It is drawn short here.