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

原创 2012年03月24日 09:31:20

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


在观察者模式中,主要涉及观察者(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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

03使用jdk提供类实现观察者模式(三)

1.jdk默认对观察者模式提供了支持    被观察着继承Observable     观察者实现Observer接口       被观察者通过调用notifyObservers...

Java8 观察者模式详解 jdk实现源码分析

Java8 观察者模式详解 jdk实现源码分析

看JSlider的jdk源码学观察者模式

有很多Swing组建都是用MVC体系结构,其中Model层结合观察者模式通知所有观察该组件的对象。看JSlider类的部分源代码(其他的Swing组件有些事类似的): public JSlider(...

自实现观察者模式

Java设计模式(观察者模式JDK自带)

JDK自带的观察者模式和我们上一节说的基本相似,也有通用的观察协议规则 Observer,这个接口中只有一个方法,就是update方法 public interface Observer { ...

观察者模式两种实现方式

  • 2017-07-14 10:43
  • 23.94MB
  • 下载

我所理解的设计模式(C++实现)——观察者模式(Observer Pattern)

概述:         最近中国股市起起伏伏,当然了起伏就用商机,小明发现商机后果断想入市,买入了中国证券,他想在电脑客户端上,网页上,手机上,iPad上都可以查看到该证券的实时行情,这种情况下我们...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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