[JAVA] Which man do you like? Identifying love targets using the Chain of Responsibility pattern

【Overview】

Programming using the Chain of Responsibility pattern of the design pattern. If the man passed as an argument likes it, it will be processed, and if it does not like it, it will be introduced to the next woman. It's okay if no woman likes it! Everyone likes Enako, so there is a helping hand. However, there is a high possibility of cheating. I'm using Java 8.

[Hierarchy]

DesignPattern → Project name    |---src | --- ChainOfResponsibility → Package name | --- Man.java (A class that holds the height and weight of men) | --- Woman.java (class for making female chains) | --- EveryoneLike.java (A class that everyone likes) | --- WeightCare.java (Class that only cares about weight) | --- HeightCare.java (Class that only cares about height) | --- NoOneLike.java (a class that no one likes) | --- Main.java (class that performs main processing for program execution)

[Class diagram]

chainOfResponsibility.png

【code】

Man.java


package ChainOfResponsibility;

public class Man {
	//name
	private String name;
	//height
	private int height;
	//body weight
	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:" + height + "cm weight:" + 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) {
		//No one likes Mio
		Woman mio = new NoOneLike("Mio");
		//Maple is a favorite for men weighing less than 50 kg
		Woman kaede = new WeightCare("Maple", 50);
		//Nagisa likes men over 170 cm tall
		Woman nagisa = new HeightCare("Nagisa", 170);
		//Everyone likes Enako
		Woman enako = new EveryoneLike("Enako");

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

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

	private static void setMan() {
		//Hiroto is 150 cm tall and weighs 47 kg
		ManList.add(new Man("Hiroto", 150, 47));
		//Naohiro is 173 cm tall and weighs 57 kg
		ManList.add(new Man("Naohiro", 173, 57));
		//Daisuke is 160 cm tall and weighs 64 kg
		ManList.add(new Man("Daisuke", 160, 64));
	}
}

【Execution result】

[Maple] likes [Hiroto Height: 150cm Weight: 47kg].
[Nagisa] likes [Naohiro Height: 173cm Weight: 57kg].
[Enako] likes [Daisuke Height: 160cm Weight: 64kg].

【reference】

[Introduction to Design Patterns Learned in the Augmented and Revised Java Language](https://www.amazon.co.jp/%E5%A2%97%E8%A3%9C%E6%94%B9%E8%A8%82% E7% 89% 88Java% E8% A8% 80% E8% AA% 9E% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% B6% E3% 82% A4% E3% 83% B3% E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3% E5% 85% A5% E9% 96% 80-% E7% B5 % 90% E5% 9F% 8E-% E6% B5% A9 / dp / 4797327030 / ref = sr_1_1? Ie = UTF8 & qid = 1515328473 & sr = 8-1 & keywords =% E3% 83% 87% E3% 82% B6% E3% 82 % A4% E3% 83% B3% E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3)

[Bonus corner]

・ Articles that were interesting with design patterns Dramatic before ◯ After-sales design pattern ~ facade pattern ~

Recommended Posts

Which man do you like? Identifying love targets using the Chain of Responsibility pattern
Chain of Responsibility Pattern
Chain Of Responsibility pattern
Design pattern ~ Chain of Responsibility ~
Are you using the default method of the interface properly?