Chain of Responsibility Pattern in Java
Java
215
Chain of Responsibility
1
Pattern
11
Male avatar

loveprogramming viết ngày 10/05/2021

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);

}

2.2- Create ProblemLevel enum:
More at:

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

Chain of Responsibility Pattern in Java

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 11 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 9 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 9 tháng trước
1 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!