关闭

观察者模式独自实现

425人阅读 评论(0) 收藏 举报

观察者的基类接口:

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:61634次
    • 积分:1037
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:17篇
    • 译文:0篇
    • 评论:14条
    最新评论