読者です 読者をやめる 読者になる 読者になる

Observerパターン

% find . -name "*.java" -print0 |xargs -0 cat                                []
public class DigitObserver implements Observer {

	@Override
	public void update(NumberGenerator generator) {
		System.out.println("DigitObserver:" + generator.getNumber());
		try {
			Thread.sleep(100);
		} catch (Exception e) {
		}
	}

}
public class GraphObserver implements Observer {

	@Override
	public void update(NumberGenerator generator) {
		System.out.print("GraphObserver:");
		for (int i = 0, l = generator.getNumber(); i < l; i++) {
			System.out.print("*");
		}
		System.out.println("");
		try {
			Thread.sleep(100);
		} catch (Exception e) {
		}
	}

}
public class Main {

	public static void main(String[] args) {
		NumberGenerator generator = new RandomNumberGenerator();
		Observer observer1 = new DigitObserver();
		Observer observer2 = new GraphObserver();
		generator.addObserver(observer1);
		generator.addObserver(observer2);
		generator.execute();
	}

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

public abstract class NumberGenerator {
	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() {
		for (Observer obs : observers) {
			obs.update(this);
		}
	}

	public abstract int getNumber();

	public abstract void execute();

}
public interface Observer {
	public abstract void update(NumberGenerator generator);
}
import java.util.Random;

public class RandomNumberGenerator extends NumberGenerator {
	private Random random = new Random();
	private int number;

	@Override
	public int getNumber() {
		return number;
	}

	@Override
	public void execute() {
		for (int i = 0; i < 20; i++) {
			number = random.nextInt(50);
			notifyObservers();
		}
	}

}
DigitObserver:42
GraphObserver:******************************************
DigitObserver:44
GraphObserver:********************************************
DigitObserver:46
GraphObserver:**********************************************
DigitObserver:15
GraphObserver:***************
DigitObserver:3
GraphObserver:***
DigitObserver:18
GraphObserver:******************
DigitObserver:13
GraphObserver:*************
DigitObserver:26
GraphObserver:**************************
DigitObserver:33
GraphObserver:*********************************
DigitObserver:35
GraphObserver:***********************************
DigitObserver:9
GraphObserver:*********
DigitObserver:29
GraphObserver:*****************************
DigitObserver:28
GraphObserver:****************************
DigitObserver:25
GraphObserver:*************************
DigitObserver:23
GraphObserver:***********************
DigitObserver:4
GraphObserver:****
DigitObserver:29
GraphObserver:*****************************
DigitObserver:24
GraphObserver:************************
DigitObserver:26
GraphObserver:**************************
DigitObserver:5
GraphObserver:*****