观察者模式独自实现

观察者的基类接口:

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 foreach循环, Iterator 迭代和remove方法之间的冲突
下一篇观察者模式理解,以及JDK实现
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭