观察者模式独自实现

原创 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 !!

JAVA设计模式 之 观察者模式(JDK内置实现)

简介:使用JAVA内置的观察者模式
  • chengzhangmsdn
  • chengzhangmsdn
  • 2014年05月17日 19:27
  • 1917

Java设计模式之观察者模式

本文继续介绍23种设计模式系列。介绍的是观察者模式。
  • jason0539
  • jason0539
  • 2015年04月16日 07:32
  • 29739

观察者模式(C语言实现)

一. 概述 Observer 模式要解决的问题为:建立一个一(Subject)对多(Observer)的依赖关系,并且做到当“一”变化的时候,依赖这个“一”的多也能够同步改变。 Sbuject...
  • HMSIWTV
  • HMSIWTV
  • 2014年09月27日 12:31
  • 3872

观察者模式的实现

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

一个利用Service和观察者模式实现断点续传下载功能的Demo

  • 2015年05月15日 10:49
  • 1.49MB
  • 下载

观察者模式C++实现

  • 2014年04月30日 15:39
  • 42KB
  • 下载

观察者模式的boost及宏实现

  • 2013年08月22日 17:32
  • 4KB
  • 下载

观察者模式 c++ 实现

  • 2010年02月05日 16:39
  • 9KB
  • 下载

ASP.NET实现的观察者模式

  • 2009年09月11日 14:52
  • 6KB
  • 下载

安卓开发观察者模式应用-一个service通知多个activity更新ui的实现

  • 2017年08月31日 18:02
  • 18.69MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:观察者模式独自实现
举报原因:
原因补充:

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