terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat chain_of_responsibility.md
guest@codeandosimple: ~/blog/design-patterns $ cat chain_of_responsibility.md

Chain of Responsibility_

// "Take the attitude of a student, never be too old to ask questions, never know too much to learn something new" - Og Mandino

# Purpose

The Chain of Responsibility pattern decouples the sender of a request from its receiver, allowing more than one object the opportunity to handle that request.

This is done by chaining the receiving objects and passing the request along the chain until it is handled.

# Problem

  • Tight Coupling: The sender needs to know the specific receiver.

  • Handling Decision: A request can be handled in different ways depending on the context.

# Solution

  • Chain of Handlers: Passes the request through a chain; each link decides whether to process it or pass it on.

  • Decoupling: The sender does not know who will resolve the request.

# Structure

Chain of Responsibility UML Structure

# Participants

  • Handler: Common interface for all handlers.
  • ConcreteHandler: Implements the handling or passes it to the successor.
  • Client: Initiates the request at the first link.

# Sequence Diagram

Sequence Diagram

Each receiver can process something before passing the request, allowing functionality to be distributed.

# When to Use It

  • The handler is not known fully in advance and must be determined automatically.

  • To issue a request to one of several objects without specifying the receiver explicitly.

# Advantages

  • verified

    Decoupling: Sender and receiver do not depend on each other.

  • verified

    Flexibility: Dynamism in who handles the request.

# Disadvantages

  • warning

    Performance: The chain can be long.

  • warning

    Not Guaranteed: It could be that no one handles the request.

# Example: Technical Support

Multi-level support system: basic, intermediate, and advanced. Each level resolves or escalates the issue.

Technical Support Example

# Java Code

abstract class SupportHandler {
    protected SupportHandler next;
    public void setNext(SupportHandler n) { next = n; }
    public abstract void handle(SupportRequest r);
}

class BasicSupport extends SupportHandler {
    public void handle(SupportRequest r) {
        if (r.getLevel() <= BASIC) System.out.println("Basic handles it");
        else if (next != null) next.handle(r);
    }
}

# Mapping Participants

  • SupportHandler (Handler): Abstract interface.
  • Basic/Intermediate/Advanced (Receiver): Concrete classes.
  • Client: Initiates the request.

# Conclusions

Each handler has the given opportunity to handle the request or pass it along the chain, allowing for a gradual and efficient resolution.

# Related Patterns

Composite

Can be combined so that parents act as successors.