Moe's Tech Blog

[Design Pattern] Chain of Responsibility Pattern 본문

Software Design Pattern/Notes

[Design Pattern] Chain of Responsibility Pattern

moe12825 2022. 8. 25. 09:34

Motivation

  • Think of requesting help on a health problem
    • you try visiting your doctor first with your quest but your case is unusal, so you are referred to a specialist
    • but the specialist is on leave and can't take your request and so you referred to another specialist
    • finally, this specialist ends up dealing with your problem
  • you don't care who along this chain of professionals help, but only that your request is met

 

Chain of Responsibility Pattern

  • is a chain of objects that are responsible for handling requests
  • in software, it's a series of handler objects that are linked together

  • Example
    • 1. Fixing a chair (you need to tighten a particular screw)
      • you don't know which type of tool to use
      • you have a several types of screw drivers and wrenches
      • So, you would
        • 1. take each tool and one at a time, try it on the screw until one of them works
      • This is very similar to how the chain of responsibility design pattern works - each object tries to handle the request until one is able to successfully handle it
    • 2. Setting up an email service where there are lots of ways to filter through the emails
      • an email message is considered by a series of filters until one is found that applies
      • you can create objects that work as individual filters
      • each of these filter objects has a method that's called handleRequest()
      • This methoid will check
        • 1. if a filter's particular rule matches an email messages content.
          • If it matches the content, it will deal with the email
          • if the rule doesn't match, then the filter will call the next filter in the chain to handle the request  
      • Using the chain of responsibility design pattern is a very common way to setup this kind of behavior

Chain of Responsibility UML Diagram

  • All the objects on the chain are handlers that implement a common method handleRequest declared in the abstract superclass handler
  • Problem:
    • 1. What if rule doesn't match but forgets to pass the request on to the next filter? The handling ends prematurly

 

How to Apply This Pattern

  • 1. Make sure the handling doesn't prematurly, make sure the following steps are followed in each Handler class:

  • 2. Chain the Handler classes using template design pattern
    • it is very common to combine multiple design patterns

 

Summary

  • Chain of responsibility 
    • is very important design pattern to learn in software
    • is a very useful pattern that can help decouple classes and deal with situations which streams of different requests need to be handled
    • has intent of avoiding coupling the sender to the receiver by giving more than one object the chance to handle the request
      • Whoever sends the request doesn't have to care who will handle the request
      • This decouples the sender and receiver from each other