Java观察者模式示例

可运行示例。

大家在平时工作和学习中都是如何运用观察者模式的,分享下,哈。。。也讨论下其主要运用场景。

上代码:

package com.gannalyo.observer;

import java.util.Observable;

/**
 * One observable tool, it means like a broadcast.
 * 
 * @author Gannalyo
 * @since 2018-07-26
 */
public class MyObservable extends Observable {
	private String data = "";

	public String getData() {
		return data;
	}

	public void setData(String data) {
		// the value will be never null for this.data
		if (data != null && !data.equals(this.data)) {
			this.data = data;
			// a mark for changing to data
			setChanged();
		}

		// to notify all observers when the data was changed
		notifyObservers();
	}

}
package com.gannalyo.observer;

import java.util.Observable;
import java.util.Observer;

/**
 * Observer
 * 
 * @author Gannalyo
 * @since 2018-07-26
 */
public class AObserver implements Observer {

	@Override
	public void update(Observable arg0, Object arg1) {
		System.out.println("The Observer A receives the new data [" + ((MyObservable) arg0).getData() + "].");
	}

}
package com.gannalyo.observer;

import java.util.Observable;
import java.util.Observer;

/**
 * Observer
 * 
 * @author Gannalyo
 * @since 2018-07-26
 */
public class BObserver implements Observer {

	@Override
	public void update(Observable arg0, Object arg1) {
		System.out.println("The Observer B receives the new data [" + ((MyObservable) arg0).getData() + "].");
	}

}
package com.gannalyo.observer;

import java.util.Observable;
import java.util.Observer;

/**
 * Observer
 * 
 * @author Gannalyo
 * @since 2018-07-26
 */
public class CObserver implements Observer {

	@Override
	public void update(Observable arg0, Object arg1) {
		System.out.println("The Observer C receives the new data [" + ((MyObservable) arg0).getData() + "].");
	}

}
package com.gannalyo.observer;

import java.util.Scanner;

/**
 * To exercise the Observer Design for Java.
 * 
 * @author Gannalyo
 * @since 2018-07-26
 */
public class ObserverDesignTesting {

	public static void main(String[] args) {
		MyObservable observer = new MyObservable();

		AObserver aObserver = new AObserver();
		BObserver bObserver = new BObserver();
		CObserver cObserver = new CObserver();

		observer.addObserver(aObserver);
		observer.addObserver(bObserver);
		observer.addObserver(cObserver);

		System.out.println("Please type your broadcast information:");
		Scanner scanner = new Scanner(System.in);
		while (true) {
			String msg = scanner.nextLine();
			if ("ok".equalsIgnoreCase(msg)) {
				System.err.println("See you!");
				break;
			} else if ("add a".equalsIgnoreCase(msg)) {// append an observer
				observer.addObserver(aObserver);
			} else if ("add b".equalsIgnoreCase(msg)) {
				observer.addObserver(aObserver);
			} else if ("add c".equalsIgnoreCase(msg)) {
				observer.addObserver(aObserver);
			} else if ("del a".equalsIgnoreCase(msg)) {// delete an observer
				observer.deleteObserver(aObserver);
			} else if ("del b".equalsIgnoreCase(msg)) {
				observer.deleteObserver(aObserver);
			} else if ("del c".equalsIgnoreCase(msg)) {
				observer.deleteObserver(aObserver);
			}

			System.out.println("Currently, the Observer count is [" + observer.countObservers() + "]。");
			observer.setData(msg);

			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		scanner.close();
	}

}

 

转载于:https://my.oschina.net/u/3901290/blog/1865312

你可能感兴趣的:(Java观察者模式示例)