Home > Java > javaTutorial > body text

Java implementation of Observer pattern instance details (picture)

黄舟
Release: 2017-03-11 11:10:57
Original
1909 people have browsed it

1. Java’s own implementation

Class diagram



##

/**
 * 观察目标 继承自 java.util.Observable
 * @author stone
 *
 */
public class UpdateObservable extends Observable {
	
	private int data;
	
	public UpdateObservable(Observer observer) {
		addObserver(observer);
		/*
		 * add other observer
		 */
	}
	
	public int getData() {
		return data;
	}
	
	public void setData(int data) {
		if (data != this.data) {
			this.data = data;
			setChanged(); //标记 改变, 只有标记后才能通知到
			notifyObservers(); //通知
		}
		
	}

	@Override
	public synchronized void addObserver(Observer o) {
		super.addObserver(o);
	}

	@Override
	public synchronized void deleteObserver(Observer o) {
		super.deleteObserver(o);
	}

	@Override
	public void notifyObservers() {
		super.notifyObservers();
	}

	@Override
	public void notifyObservers(Object arg) {
		super.notifyObservers(arg);
	}

	@Override
	public synchronized void deleteObservers() {
		super.deleteObservers();
	}

	@Override
	protected synchronized void setChanged() {
		super.setChanged();
	}

	@Override
	protected synchronized void clearChanged() {
		super.clearChanged();
	}

	@Override
	public synchronized boolean hasChanged() {
		return super.hasChanged();
	}

	@Override
	public synchronized int countObservers() {
		return super.countObservers();
	}
	
}
Copy after login


/**
 * 观察者	实现 java.util.Observer接口
 * @author stone
 *
 */
public class UpdateObserver implements Observer {

	@Override
	public void update(Observable o, Object arg) {
		System.out.println("接收到数据变化的通知:");
		if (o instanceof UpdateObservable) {
			UpdateObservable uo = (UpdateObservable) o;
			System.out.print("数据变更为:" + uo.getData());
		}
	}

}
Copy after login

2. Customized observation model

Class diagram



/**
 * 抽象观察者	Observer
 * 观察 更新		
 * @author stone
 *
 */
public interface IWatcher {
	/*
	 * 通知接口:
	 * 1. 简单通知
	 * 2. 观察者需要目标的变化的数据,那么可以将目标用作参数, 见Java的Observer和Observable
	 */
//	void update(IWatched watched);
	
	void update();
	
}
Copy after login


/**
 * 抽象目标	Subject
 * 	提供注册和删除观察者对象的接口, 及通知观察者进行观察的接口
 *  及目标 自身被观察的业务的接口
 * @author stone
 *
 */
public interface IWatchedSubject {
	
	public void add(IWatcher watch);

	public void remove(IWatcher watch);
	
	public void notifyWhatchers();
	
	public void update();//被观察业务变化的接口
}
Copy after login


/**
 * 具体观察者		Concrete Observer
 * 
 * @author stone
 *
 */
public class UpdateWatcher implements IWatcher {

	@Override
	public void update() {
		System.out.println(this + "观察到:目标已经更新了");
	}

}
Copy after login


/**
 * 具体目标角色	Concrete Subject
 * @author stone
 *
 */
public class UpdateWatchedSubject implements IWatchedSubject {
	private List<IWatcher> list;
	
	public UpdateWatchedSubject() {
		this.list = new ArrayList<IWatcher>();
	}
	
	@Override
	public void add(IWatcher watch) {
		this.list.add(watch);
	}

	@Override
	public void remove(IWatcher watch) {
		this.list.remove(watch);
	}

	@Override
	public void notifyWhatchers() {
		for (IWatcher watcher : list) {
			watcher.update();
		}
	}
	
	@Override
	public void update() {
		System.out.println("目标更新中....");
		notifyWhatchers();
	}

}
Copy after login

The listener is an implementation of the observer

Class diagram


/**
 * 监听 用户在注册后
 * @author stone
 *
 */
public interface IRegisterListener {
	void onRegistered();
}
Copy after login
/**
 * 监听 当用户登录后
 * @author stone
 *
 */
public interface ILoginListener {
	void onLogined();
}
Copy after login
/*
 * 监听器 是观察者模式的一种实现
 * 一些需要监听的业务接口上添加 监听器,调用监听器的相应方法,实现监听
 */
public class User {
	
	public void register(IRegisterListener register) {
		/*
		 * do ... register
		 */
		System.out.println("正在注册中...");
		//注册后
		register.onRegistered();
	}

	public void login(ILoginListener login) {
		/*
		 * do ... login
		 */
		System.out.println("正在登录中...");
		//登录后
		login.onLogined();
	}

}
Copy after login
/**
 * 观察者(Observer)模式	行为型模式
 *   观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时观察某一个目标对象。
 *   这个目标对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己
 * 目标对象中需要有添加、移除、通知 观察者的接口
 *   
 * @author stone
 */
public class Test {
	
	public static void main(String[] args) {
		/*
		 * 使用Java自带的Observer接口和Observable类
		 */
		UpdateObservable observable = new UpdateObservable(new UpdateObserver());
		observable.setData(99);
		System.out.println("");
		System.out.println("");
		/*
		 * 自定义的观察者模型
		 */
		IWatchedSubject watched = new UpdateWatchedSubject();
		watched.add(new UpdateWatcher());
		watched.add(new UpdateWatcher());
		watched.update();
		System.out.println("");
		
		/*
		 * 子模式-监听器
		 */
		
		User user = new User();
		user.register(new IRegisterListener() {
			
			@Override
			public void onRegistered() {
				System.out.println("监听到注册后。。。");
			}
		});
		user.login(new ILoginListener() {
			
			@Override
			public void onLogined() {
				System.out.println("监听到登录后。。。");
			}
		});
		
	}
}
Copy after login

Print

接收到数据变化的通知:
数据变更为:99

目标更新中....
observer.UpdateWatcher@457471e0观察到:目标已经更新了
observer.UpdateWatcher@5fe04cbf观察到:目标已经更新了

正在注册中...
监听到注册后。。。
正在登录中...
监听到登录后。。。
Copy after login

The above is the detailed content of Java implementation of Observer pattern instance details (picture). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template