Takashi! I've got rice! Notification of rice using the Ovserver pattern

【Overview】

Programming using the ʻObserver` pattern of the design pattern. Notify "Takashi" and "Climb" that breakfast, lunch and dinner are ready. I'm using Java 8.

[Hierarchy]

DesignPattern → Project name    |---src | --- Observer → Package name | --- Observer.java (interface to represent the observer) | --- ChildObserver.java (class to notify that rice is ready) | --- CookGenerator.java (abstract class for making rice) | --- BreakfastGenerator.java (Breakfast making class) | --- LunchGenerator.java (Lunch making class) | --- DinnerGenerator.java (Supper making class) | --- Main.java (class that performs main processing for program execution)

[Class diagram]

observer.png

【code】

Observer.java


package Observer;

public interface Observer {
	public abstract void update(CookGenerator generator);
}

ChildObserver.java


package Observer;

public class ChildObserver implements Observer {
	private String name;

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

	public void update(CookGenerator generator) {
		System.out.println(name + "! " + generator + "I was able to do it!");
	}
}

CookGenerator.java


package Observer;

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

public abstract class CookGenerator {
	private List<Observer> observers = new ArrayList<>();

	public void addObserver(Observer observer) {
		observers.add(observer);
	}

	//I'm not using this method
	public void deleteObserver(Observer observer) {
		observers.remove(observer);
	}

	public void notifyObservers() {
		Iterator it = observers.iterator();
		while (it.hasNext()) {
			Observer o = (Observer)it.next();
			o.update(this);
		}
	}

	public abstract void cook();

}

BreakfastGenerator.java


package Observer;

public class BreakfastGenerator extends CookGenerator {
	@Override
	public void cook() {
		notifyObservers();
	}

	@Override
	public String toString() {
		return "breakfast";
	}
}

LunchGenerator.java


package Observer;

public class LunchGenerator extends CookGenerator {
	@Override
	public void cook() {
		notifyObservers();
	}

	@Override
	public String toString() {
		return "lunch";
	}
}

DinnerGenerator.java


package Observer;

public class DinnerGenerator extends CookGenerator {
	@Override
	public void cook() {
		notifyObservers();
	}

	@Override
	public String toString() {
		return "Dinner";
	}
}

Main.java


package Observer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Main {

	public static void main(String[] args) throws Exception {
		List<CookGenerator> generatorList = new ArrayList<>();

		CookGenerator breakfast = new BreakfastGenerator();
		CookGenerator lunch = new LunchGenerator();
		CookGenerator dinner = new DinnerGenerator();
		generatorList.add(breakfast);
		generatorList.add(lunch);
		generatorList.add(dinner);

		Observer takashi = new ChildObserver("Takashi");
		Observer noboru = new ChildObserver("Climb");

		for (CookGenerator generator : generatorList) {
			generator.addObserver(takashi);
			generator.addObserver(noboru);
		}

		for (CookGenerator generator : generatorList) {
			generator.cook();
			TimeUnit.SECONDS.sleep(2);
		}
	}
}

【Execution result】

Takashi! Breakfast was ready!
Climb! Breakfast was ready!
Takashi! Lunch was ready!
Climb! Lunch was ready!
Takashi! The dinner was ready!
Climb! The dinner was ready!

【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]

・ A collection of design patterns found on GitHub   java-design-patterns

Recommended Posts

Takashi! I've got rice! Notification of rice using the Ovserver pattern
Try using || instead of the ternary operator
Test the integrity of the aggregation using ArchUnit ②
Now, I've summarized the basics of RecyclerView
Test the integrity of the aggregation using ArchUnit ③
Send a notification to slack with the free version of sentry (using lambda)