Help us understand the problem. What is going on with this article?

どの男性が好み? Chain of Responsibilityパターンを使用した恋愛対象の洗い出し

More than 1 year has passed since last update.

【概要】

デザインパターンのChain of Responsibilityパターンを使用したプログラミングです。
引数で渡された男性が好みなら処理をする、好みでない場合は次の女性へ紹介します。
どの女性からも好かれない場合でも大丈夫!エナコちゃんだけは誰でも好きだから救いの手はあります。ただし、浮気する可能性は高いかもー。
Java8を使用しています。

【階層図】

DesignPattern → プロジェクト名
   |---src
     |---ChainOfResponsibility → パッケージ名
          |---Man.java(男性の身長と体重を保持するクラス)
          |---Woman.java(女性の連鎖を作るためのクラス)
          |---EveryoneLike.java(誰でも好きになるクラス)
          |---WeightCare.java(体重だけ気にするクラス)
          |---HeightCare.java(身長だけ気にするクラス)
          |---NoOneLike.java(誰も好きにならないクラス)
          |---Main.java(プログラム実行用のメイン処理を行うクラス)

【クラス図】

chainOfResponsibility.png

【コード】

Man.java
package ChainOfResponsibility;

public class Man {
    // 名前
    private String name;
    // 身長
    private int height;
    // 体重
    private int weight;

    public Man(String name, int height, int weight) {
        this.name = name;
        this.height = height;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public int getHeight() {
        return height;
    }

    public int getWeight() {
        return weight;
    }

    public String toString() {
        return "[" + name + " 身長:" + height + "cm 体重:" + weight + "kg]";
    }
}
Woman.java
package ChainOfResponsibility;

public abstract class Woman {
    private String name;
    private Woman next;

    public Woman(String name) {
        this.name = name;
    }

    public Woman setNext(Woman next) {
        this.next = next;
        return next;
    }

    public void attackedByMan(Man man) {
        if(tryLove(man)) {
            like(man);
        } else if (next != null) {
            next.attackedByMan(man);
        } else {
            dislike(man);
        }
    }

    public String toString() {
        return "[" + name + "]";
    }

    protected abstract boolean tryLove(Man man);

    protected void like(Man man) {
        System.out.println(this + " likes " + man + ".");
    }

    protected void dislike(Man man) {
        System.out.println(this + " doesn't likes " + man + ".");
    }
}
EveryoneLike.java
package ChainOfResponsibility;

public class EveryoneLike extends Woman {
    public EveryoneLike(String name) {
        super(name);
    }

    @Override
    protected boolean tryLove(Man man) {
        return true;
    }
}
HeightCare.java
package ChainOfResponsibility;

public class HeightCare extends Woman {
    private int height;

    public HeightCare(String name, int height) {
        super(name);
        this.height = height;
    }

    protected boolean tryLove(Man man)   {
        return height <= man.getHeight();
    }
}
WeightCare.java
package ChainOfResponsibility;

public class WeightCare extends Woman {
    private int weight;

    public WeightCare(String name, int weight) {
        super(name);
        this.weight = weight;
    }

    @Override
    protected boolean tryLove(Man man) {
        return man.getWeight() <= weight;
    }
}
NoOneLike.java
package ChainOfResponsibility;

public class NoOneLike extends Woman {
    public NoOneLike(String name) {
        super(name);
    }

    @Override
    protected boolean tryLove(Man man) {
        return false;
    }
}
Main.java
package ChainOfResponsibility;

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static List<Man> ManList = new ArrayList<>();

    public static void main(String[] args) {
        // ミオは誰も好きではない
        Woman mio = new NoOneLike("ミオ");
        // カエデは体重50kg以下の男なら好き
        Woman kaede = new WeightCare("カエデ", 50);
        // ナギサは身長170cm以上の男なら好き
        Woman nagisa = new HeightCare("ナギサ", 170);
        // エナコは誰でも好き
        Woman enako = new EveryoneLike("エナコ");

        mio.setNext(kaede).setNext(nagisa).setNext(enako);
        setMan();

        for (Man man : ManList) {
            mio.attackedByMan(man);
        }
    }

    private static void setMan() {
        // ヒロトは身長150cm、体重47kg
        ManList.add(new Man("ヒロト", 150, 47));
        // ナオヒロは身長173cm、体重57kg
        ManList.add(new Man("ナオヒロ", 173, 57));
        // ダイスケは身長160cm、体重64kg
        ManList.add(new Man("ダイスケ", 160, 64));
    }
}

【実行結果】

[カエデ] likes [ヒロト 身長:150cm 体重:47kg].
[ナギサ] likes [ナオヒロ 身長:173cm 体重:57kg].
[エナコ] likes [ダイスケ 身長:160cm 体重:64kg].

【参考】

増補改訂版Java言語で学ぶデザインパターン入門

【おまけのコーナー】

・デザインパターンで面白かった記事
  劇的ビフォ◯アフターで学ぶデザインパターン〜facade pattern〜

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away