关闭

观察者模式理解,以及JDK实现

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

观察者模式(有时又被称为发布/订阅模式)。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实作事件处理系统。


在观察者模式中,主要涉及观察者(Observer) 和被观察的对象(Observerable)。

需要指出的重要一点:在每个Observerable对象内部,都维护者一个对外private的观察者对象的列表。只能通过addObserver方法,将Observer添加到这个列表中。Observaerable对象发生改变的时候,通过调用setChange方法,告诉Observer对象,Observerable对象发生了改变。然后调用Observerable对象继承来的notify的相关方法,唤醒Observer对象,执行Observer的update方法。


调用JDK中的java.util包中的类的实现如下。

package viekie.du.Model;

import java.util.Observable;

public class Student extends Observable {

	private int age;
	
	private String name;
	
	public Student(int age, String name) {
		this.age = age;
		this.name = name;
	}
	
	public void getOlder() {
		
		int currentAge = this.age;
		
		while ( age < 18 ) {
			age ++;
			
			if( currentAge != this.age ) {
				
				this.setChanged();
				this.notifyObservers(name + "is growing, he's age is " + this.age);
			}
		}
		
		
	}
}

package viekie.du.Model;

import java.util.Observable;
import java.util.Observer;

public class Teacher implements Observer{

	private String name;
	
	public Teacher(String name) {
		this.name = name;
	}
	
	@Override
	public void update(Observable obser, Object obj) {

		System.out.println(obj.toString());
	}

}

package Main;

import viekie.du.Model.Student;
import viekie.du.Model.Teacher;

public class MainTest {

	public static void main(String[] args ) {
		
		Student liaoliang = new Student(6, "liaoliang");
		Student wangxiaoyong = new Student(6, "wangxiaoyong");
		Student luoqing = new Student(6, "luoqing");
		
		Teacher viekieSir = new Teacher("viekie");
		
		liaoliang.addObserver(viekieSir);
		wangxiaoyong.addObserver(viekieSir);
		luoqing.addObserver(viekieSir);
		
	
		liaoliang.getOlder();
		wangxiaoyong.getOlder();
		luoqing.getOlder();
		
		
	}
}

输出的内容如下:

liaoliangis growing, he's age is 7
liaoliangis growing, he's age is 8
liaoliangis growing, he's age is 9
liaoliangis growing, he's age is 10
liaoliangis growing, he's age is 11
liaoliangis growing, he's age is 12
liaoliangis growing, he's age is 13
liaoliangis growing, he's age is 14
liaoliangis growing, he's age is 15
liaoliangis growing, he's age is 16
liaoliangis growing, he's age is 17
liaoliangis growing, he's age is 18
wangxiaoyongis growing, he's age is 7
wangxiaoyongis growing, he's age is 8
wangxiaoyongis growing, he's age is 9
wangxiaoyongis growing, he's age is 10
wangxiaoyongis growing, he's age is 11
wangxiaoyongis growing, he's age is 12
wangxiaoyongis growing, he's age is 13
wangxiaoyongis growing, he's age is 14
wangxiaoyongis growing, he's age is 15
wangxiaoyongis growing, he's age is 16
wangxiaoyongis growing, he's age is 17
wangxiaoyongis growing, he's age is 18
luoqingis growing, he's age is 7
luoqingis growing, he's age is 8
luoqingis growing, he's age is 9
luoqingis growing, he's age is 10
luoqingis growing, he's age is 11
luoqingis growing, he's age is 12
luoqingis growing, he's age is 13
luoqingis growing, he's age is 14
luoqingis growing, he's age is 15
luoqingis growing, he's age is 16
luoqingis growing, he's age is 17
luoqingis growing, he's age is 18


0
0

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