More than 3 years have passed since last update.

posted at

updated at

Chain of Responsibility

Chain of Responsibility

In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects.


  • define a chain of receiver having a responsibility, handle a request or if it's not able to handle a request, forward it the next receiver on the chain.
  • not need to be conscious of a role of controller like which object handle which request.
  • user only throw a request to one object on the chain.



This is an application to judge your character by color you like.
Color.java is a Handler class.
Green.java is a receiver class.
Main.java is a sender class.

public abstract class Color {

    private Color next;
    private ColorEnum color;

    public Color(ColorEnum color) {
        this.color = color;

    public Color setNext(Color next) {
        this.next = next;
        return next;

    public void selectColor (ColorEnum color) {
        if (beAbleToJudge(color)) {
        } else if (next != null) {
        } else {
            System.out.println("The color you selected is invalid. Please select other color!");

    protected boolean beAbleToJudge (ColorEnum color) {
        if (this.color.equalWithColor(color)) {
            return true;
        } else {
            return false;

    protected abstract void showCharacter (ColorEnum color);
public class Green extends Color {

    public Green (ColorEnum color) {

    protected void showCharacter (ColorEnum color) {
        System.out.println("You selected Green.");
        System.out.println("Your character is below");
        System.out.println("#2.judicious and delicate");
public enum ColorEnum {

    , YELLOW
    , PINK
    , RED
    , ORANGE
    , BLACK
    , BROWN
    , GREY
    , BLUE
    , WHITE
    , PURPLE;

    public boolean equalWithColor (ColorEnum color) {
        if (this.equals(color)) {
            return true;
        } else {
            return false;
public class Person {

    private String name;
    private ColorEnum color;

    public Person (String name, ColorEnum color) {
        this.name = name;
        this.color = color;

    public String getName() {
        return name;

    public ColorEnum getColor() {
        return color;
public class Main {

    public static void main(String[] args) {

        List<Person> list = new ArrayList<Person>();

        Color green = new Green(ColorEnum.GREEN);
        Color yellow = new Yellow(ColorEnum.YELLOW);
        Color pink = new Pink(ColorEnum.PINK);
        Color red = new Red(ColorEnum.RED);
        Color orange = new Orange(ColorEnum.ORANGE);
        Color black = new Black(ColorEnum.BLACK);

        for (Person person: list) {
            System.out.println("Hi " + person.getName() + "!");

    // each person select their favorite color
    public static void setPerson(List<Person> list) {
        list.add(new Person("satoshi", ColorEnum.ORANGE));
        list.add(new Person("miwa", ColorEnum.RED));
        list.add(new Person("taro", ColorEnum.GREEN));
        list.add(new Person("hitoshi", ColorEnum.YELLOW));
        list.add(new Person("eri", ColorEnum.BLACK));
        list.add(new Person("richard", ColorEnum.BROWN));
        list.add(new Person("rio", ColorEnum.PINK));
Hi satoshi!
You selected Orange.
Your character is below
#2.like wide and shallow human relations
Hi miwa!
You selected Red.
Your character is below
#1.positive and very active
#2.your emotional ups and downs are erratic
Hi taro!
You selected Green.
Your character is below
#2.judicious and delicate
Hi hitoshi!
You selected Yellow.
Your character is below
#1.curious and a strong upward mobility
#2.thinking that hierarchical relationship is important
#3.getting bored easily
Hi eri!
You selected Black.
Your character is below
#2.tend not to like to follow someone
Hi richard!
The color you selected is invalid. Please select other color!
Hi rio!
You selected Pink.
Your character is below
#1.gentle and stable
#2.strong dependence


Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up