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

Implementation

The desk, part by part, in four languages.

The point: write the part that must not be got wrong, the passing on, exactly once.

The support desk, part by part

What travels through the chain.

from dataclasses import dataclassfrom enum import IntEnumclass Priority(IntEnum):    UNKNOWN = 0    LOW = 1    MEDIUM = 2    HIGH = 3    CRITICAL = 4@dataclass(frozen=True)class Ticket:    id: int    text: str    priority: Priorityclass Unhandled(Exception):    """The ticket reached the end of the chain."""

Lines 5 to 10

An ordered enum. Engineering can say HIGH or more, and a typing error such as hihg cannot exist.

1 of 4

Why it is built this way

  • No Handler types in the ticket: the request must not know who works on it.