DEV Community

loizenai
loizenai

Posted on

Chain of Responsibility Pattern in Java

https://grokonez.com/design-pattern/chain-of-responsibility-pattern-in-java

Chain of Responsibility Pattern in Java

Chain of Responsibility Pattern is a Behavioral Pattern in which, a Request is sent through a chain of handling Objects without knowing which Object will handle it. With Chain of Responsibility Pattern, we can avoid coupling the Sender of a request to received Objects and create a processing sequence automatically based on type of request.

I. OVERVIEW

Chain of Responsibility Pattern defines a set of Handlers that receive request from Sender. Each Handler processes it if possible or pass to another Handler, or does both: process and pass it on.
chain-of-responsibility-overview-
This is a simple diagram of the Pattern:
chain-of-responsibility-diagram

  • We have an Abstract Handler class that contains inner Successor and handle method for processing Request and pass it to another Successor.
  • All Handlers extend Abstract Handler class. Each Handler sets its own Successor (the next Handler).
  • Client just gives Request for the first Handler to process, next Handler will handle Request automatically anytime it receives.

    II. PRACTICE

    1. Project Overview

    The customer has a problem with a specific level of difficulty. We have 3 people with different standards: Regular Developer, Senior and Expert. Depending on the problem level, we choose person to give customer advice. So we pass problem (level) to Regular Developer first, if he is not qualified enough to solve the problem (his level is not equal problem level), he will pass the problem to people who has higher standard, and so on. chain-of-responsibility-demo

    2. Step by Step

    2.1- Create AbstractConsultant class:
    
    package com.javasampleapproach.chainofresponsibility.pattern;

public abstract class AbstractConsultant {

protected int level;

protected AbstractConsultant nextConsultant;

public void setNextConsultant(AbstractConsultant nextConsultant) {
    this.nextConsultant = nextConsultant;
}

public void giveAdvice(int level) {

    if (this.level >= level) {
        advise(level);
    } else {
        nextConsultant.giveAdvice(level);
    }
}

abstract protected void advise(int level);
Enter fullscreen mode Exit fullscreen mode

}

2.2- Create ProblemLevel enum:
More at:

https://grokonez.com/design-pattern/chain-of-responsibility-pattern-in-java

Chain of Responsibility Pattern in Java

Top comments (0)