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

たかしー!ご飯ができたわよー! Ovserverパターンを使用したご飯の通知

More than 1 year has passed since last update.

【概要】

デザインパターンのObserverパターンを使用したプログラミングです。
「たかし」と「のぼる」に朝食、昼食、夕食ができたことを通知します。
Java8を使用しています。

【階層図】

DesignPattern → プロジェクト名
   |---src
     |---Observer → パッケージ名
          |---Observer.java(観察者を表現するインターフェース)
          |---ChildObserver.java(ご飯ができたことを通知するクラス)
          |---CookGenerator.java(ご飯を作る抽象クラス)
          |---BreakfastGenerator.java(朝食を作るクラス)
          |---LunchGenerator.java(昼食を作るクラス)
          |---DinnerGenerator.java(夕食を作るクラス)
          |---Main.java(プログラム実行用のメイン処理を行うクラス)

【クラス図】

observer.png

【コード】

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 + "ができたわよー!");
    }
}
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);
    }

    // このメソッドは使用していません
    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 "朝ごはん";
    }
}
LunchGenerator.java
package Observer;

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

    @Override
    public String toString() {
        return "昼ごはん";
    }
}
DinnerGenerator.java
package Observer;

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

    @Override
    public String toString() {
        return "夕ごはん";
    }
}
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("たかし");
        Observer noboru = new ChildObserver("のぼる");

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

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

【実行結果】

たかし! 朝ごはんができたわよー!
のぼる! 朝ごはんができたわよー!
たかし! 昼ごはんができたわよー!
のぼる! 昼ごはんができたわよー!
たかし! 夕ごはんができたわよー!
のぼる! 夕ごはんができたわよー!

【参考】

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

【おまけのコーナー】

・GitHubで見つけたデザインパターン集
  java-design-patterns

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