1、观察者模式:定义了对象间的一对多依赖,当一个对象改变状态时,它的所有依赖者能自动得到通知并更新。(出版
者+订阅者=观察者模式)
2、java.util.observer和java.util.observable提供内置的观察者模式支持,可以使用推和拉的方 式传送数据,注
意:不要依赖它的通知次序。其实现步骤如下:
1>.将对象变为观察者:
<1>.实现观察者接口(java.util.observer)
<2>.调用addobserver()方法即将对象变为观察者
<3>.如想退出观察者,调用deleteobserver()即可
2>.主题发送通知:
<1>.调用setchanged()方法,标记状态已改变
<2>.调用notifyobservers():
notifyobservers()或notifyobservers(object o)(其中o表示要传送的数据对象)
3>.观察者接受通知:
update(observable o,object o)
3、实现一个完整的观察者模式示例(推模式):不使用java的内置api
1>.设计uml图:

2>.coding
<1>.主题部分
//subject interface
public interface subject{
public void registerobserver(observer observer);
public void removeobserver(observer observer);
public void notifyobservers();
}
//mapseries:implements subject interface
import java.util.arraylist;
public class mapseries implements subject{
private arraylist<observer> observers;
private string name;
private string author;
private double price;
public mapseries(){
observers = new arraylist<observer>();
}
public void registerobserver(observer observer){
observers.add(observer);
public void removeobserver(observer observer){
int i = observers.indexof(observer);
if(i > -1){
observers.remove(i);
}
public void notifyobservers(){
for(int i = 0; i < observers.size(); i++){
observer observer = observers.get(i);
observer.update(name,author,price);
}
public void autonotify(){
notifyobservers();
public void addbook(string name, string author, double price){
this.name = name;
this.author = author;
this.price = price;
autonotify();
<2>.观察者部分
//observer interface
public interface observer{
public void update(string name,string author,double price);
//display interface
public interface display{
public void display();
//display name and author
public class nameandauthor implements observer,display{
private subject subject;
public nameandauthor(subject subject){
this.subject = subject;
subject.registerobserver(this);
public void update(string name,string author,double price){
display();
public void display(){
system.out.println("name: " + name + "\n" + "author: " + author);
//display name and price
public class nameandprice implements observer,display{
public nameandprice(subject subject){
this.name = name;
this.price = price;
system.out.println("name: " + name + "\n" + "price: " + string.valueof(price));
//display name author price
public class nameauthorprice implements observer,display{
public nameauthorprice(subject subject){
this.author =author;
system.out.println("name: " + name + "\n"
+ "author: " + author + "\n"
+ "price: " + string.valueof(price) + "\n");
<3>.测试部分
//test this application program
public class testobserver{
public static void main(string[] args){
mapseries map = new mapseries();
nameandauthor na = new nameandauthor(map);
nameandprice np = new nameandprice(map);
nameauthorprice nap = new nameauthorprice(map);
map.addbook("head first design pattern","bufubuxing",98.00);
map.addbook("head first servlet&jsp","wenlong.meng",99.99);
map.addbook("head first ejb","dragon",87.00);
<4>.测试结果部分
4、运用java内置观察者模式支持示例:拉模式
1、可观察者部分
//mapseries:extend observable class
import java.util.observable;
public class mapseries extends observable{
public mapseries(){}
setchanged();
notifyobservers();//"拉"模式:后台调用notifyobservers(null)
public string getname(){
return name;
public string getauthor(){
return author;
public double getprice(){
return price;
2>.观察者部分
import java.util.observer;
observable observable;
public nameandauthor(observable observable){
this.observable = observable;
observable.addobservser(this);
public void update(observable obs,object org){
if(obs instanceof mapseries){
mapseries map = (mapseries)obs;
this.name = map.getname();
this.author = map.getauthor();
display();
private observable observable;
public nameauthorprice(observable observable){
public void update(observable observable, object arg){
mapseries map = (mapseries)obs;
this.name = map.getname();
this.price = map.getprice();
display();
public nameandprice(observable observable){
3>.测试部分
nameauthorprice nap = new nameauthorprice(map);
map.addbook("head first design pattern","bufubuxing",98.00);
map.addbook("head first servlet&jsp","wenlong.meng",99.99);
4>.结果部分
5、运用java内置观察者模式支持示例:推模式
arraylist al = new arraylist();
al.add(name);
al.add(author);
al.add(price);
notifyobservers(al);
//notifyobservers();//
this.author = author;
/*public string getname(){
return name;
}*/
this.observable = observable;
observable.addobserver(this);
if(org != null){
name = (string)(((arraylist)org).get(0));
author = (string)(((arraylist)org).get(1));
}
/*if(obs instanceof mapseries){
mapseries map = (mapseries)obs;
this.name = map.getname();
this.author = map.getauthor();
}*/
system.out.println("name: " + name + "\n" + "author: " + author + "\n");
observable.addobserver(this);
public void update(observable obs, object org){
if(org != null){
name = (string)(((arraylist)org).get(0));
author = (string)(((arraylist)org).get(1));
price = ((double)(((arraylist)org).get(2))).doublevalue();
display();
}
/*if(obs instanceof mapseries){
mapseries map = (mapseries)obs;
this.name = map.getname();
this.author = map.getauthor();
this.price = map.getprice();
}*/
+ "author: " + author + "\n"
+ "price: " + string.valueof(price) + "\n");
public nameandprice(observable observable){
this.observable = observable;
this.price = map.getprice();
display();
system.out.println("name: " + name + "\n" + "price: " + string.valueof(price) + "\n");
mapseries map = new mapseries();
nameandauthor na = new nameandauthor(map);
nameandprice np = new nameandprice(map);
nameauthorprice nap = new nameauthorprice(map);
map.addbook("head first design pattern","bufubuxing",98.00);
map.addbook("head first servlet&jsp","wenlong.meng",99.99);
map.addbook("head first ejb","dragon",87.00);