DesignPatterns
ChainOfResponsibility

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.

Overview

  • 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.

implementation

ソースはこちら

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.

Color.java
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)) {
            showCharacter(color);
        } else if (next != null) {
            next.selectColor(color);
        } 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);
}
Green.java
public class Green extends Color {

    public Green (ColorEnum color) {
        super(color);
    }

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

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

    public boolean equalWithColor (ColorEnum color) {
        if (this.equals(color)) {
            return true;
        } else {
            return false;
        }
    }
}
Person.java
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;
    }
}
Main.java
public class Main {

    public static void main(String[] args) {

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

        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);
        green.setNext(yellow).setNext(pink).setNext(red).setNext(orange).setNext(black);

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

    // 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));
    }
}
result.md
Hi satoshi!
You selected Orange.
Your character is below
#1.active
#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
#1.conservative
#2.judicious and delicate
#3.talkative
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
#1.sensitive
#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

Reference