观察者模式独自实现

原创 2012年03月23日 22:30:30

观察者的基类接口:

package viekie.du.interfaces;

public interface Observer {

	public void update(String string);
}

被观察对象的基类接口:

package viekie.du.interfaces;

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

public class Observerable {

	protected List<Observer> observerList = new  ArrayList<Observer>();
	
	public void setChanged(){
		
	}
	
	public void notifyAllObserver(){
		
	}
	
	public void addObserver(Observer o) {
		this.observerList.add(o);
	}
}

观察者类的实现:

package viekie.du.classes;

import MainPackage.SchoolClass;
import viekie.du.interfaces.Observer;


public class Teacher implements Observer {
	
	@Override
	public void update(String string) {
		System.out.println(string);
	}

}

被观察对象的实现:

package viekie.du.classes;

import viekie.du.interfaces.Observer;
import viekie.du.interfaces.Observerable;

public class Students extends Observerable implements Runnable{

	
	private int status = 0;
	private String name;
	
	public Students(String name, int status) {
		
		this.status = status;
		this.name = name;
		
	}
	
	@Override
	public void run() {
		
		int oldStatus = this.status;
		
		setChanged();
		
		
	}

	@Override
	public void setChanged() {
		
		for ( int i = 0; i < 10; i ++ ) {
				this.status  = i;
				this.notifyAllObserver();
		}		
	}

	@Override
	public void notifyAllObserver() {
		
		if(this.observerList.size() == 0) {
			System.out.println("there is no teacher, let's go!!");
			return;
		}
		
		for (Observer observer : observerList) {
			observer.update(this.name + " is moving, it's status is " + this.status + ", Ok, Let's go, catch this little bugs !!");
		}
	}
}

测试类:

package MainPackage;
import viekie.du.classes.Students;
import viekie.du.classes.Teacher;


public class SchoolClass implements Runnable{

	public SchoolClass () {

		Teacher viekieSir = new Teacher();
		
		Students liaoliang = new Students("liaoliang",  0);
		liaoliang.addObserver(viekieSir);
		Thread liaoliangThread = new Thread(liaoliang);
		
		Students wangxiaoyong = new Students("wangxiaoyong",  2);
		wangxiaoyong.addObserver(viekieSir);
		Thread wangxiaoyongThread = new Thread(wangxiaoyong);
		
		Students luoqing = new Students("luoqing", 6);
		luoqing.addObserver(viekieSir);
		Thread luoqingThread = new Thread(luoqing);
		
		liaoliangThread.start();
		wangxiaoyongThread.start();
		luoqingThread.start();
		
	}
	public static void main(String[] args ) {
		
		new SchoolClass();
	}

	@Override
	public void run() {
		
		try {
			Thread.sleep(30);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

输出结果:

liaoliang is moving, it's status is 0, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 0, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 0, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 1, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 1, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 2, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 1, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 3, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 2, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 4, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 2, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 5, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 3, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 6, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 3, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 7, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 4, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 8, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 4, Ok, Let's go, catch this little bugs !!
wangxiaoyong is moving, it's status is 9, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 5, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 6, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 5, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 7, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 6, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 8, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 7, Ok, Let's go, catch this little bugs !!
liaoliang is moving, it's status is 9, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 8, Ok, Let's go, catch this little bugs !!
luoqing is moving, it's status is 9, Ok, Let's go, catch this little bugs !!

相关文章推荐

自实现观察者模式

  • 2012年09月21日 12:00
  • 5KB
  • 下载

观察者模式两种实现方式

  • 2017年07月14日 10:43
  • 23.94MB
  • 下载

从观察者模式,简单实现Node的Events模块

简介:观察者模式或者说订阅模式,它定义了对象间的一种一对多的关系,让多个观察者对象同时监听某一个主题对象,当一个对象发生改变时,所有依赖于它的对象都将得到通知。 node中的Events模块就是通过观...
  • liwusen
  • liwusen
  • 2017年04月27日 20:33
  • 262

Android观察者模式简单实现Demo

  • 2016年02月17日 23:52
  • 1.22MB
  • 下载

【Lua】观察者模式在Lua中的实现

--LUA对于观察者模式的实现 --被观察者表 Subject = {} --类似父类,具体对于lua面向对象编程思想学习看这里: --http://www.jb51.net/article/557...
  • poda_
  • poda_
  • 2016年02月01日 11:13
  • 544

观察者模式的实现

  • 2014年12月08日 21:30
  • 1KB
  • 下载

RCTDeviceEventEmitter 实现简单的观察者模式

RCTDeviceEventEmitter 实际上和类似于ios下的notification。初期demo使用的场景在于用户登录界面,当侦测到所有的输入框都有内容后就通知button来更改image。...

观察者模式 c++ 实现

  • 2010年02月05日 16:39
  • 9KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:观察者模式独自实现
举报原因:
原因补充:

(最多只允许输入30个字)