当前位置:首页 » 其他

设计模式---观察者模式(学习笔记)

2015-07-29 18:55 本站整理 浏览(2)

定义观察者模式:当一个主题对象(或者可以在浏览器)当状态更改。其他吸毒者(观察员)您将会收到通知。下面Demo它是基于Java jar包观察者写:

对象的代码部分:

import java.util.Observable;

public class ObserverSubject extends Observable {

	public ObserverSubject() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void sendNotify(String content){
		setChanged();
		notifyObservers(content);
	}
}
两个依赖者的代码部分:

依赖者一的代码:

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

public class ReceverOne implements Observer {

	Observable observable;
	
	public ReceverOne(Observable observable) {
		super();
		this.observable = observable;
		this.observable.addObserver(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		if(o instanceof ObserverSubject){
			   System.out.println("I am ReceverOne,I have receiver:"+arg.toString());
		}
     
	}

}
依赖者二的代码
import java.util.Observable;
import java.util.Observer;

public class ReceverTwo implements Observer {

	Observable observable;
	
	public ReceverTwo(Observable observable) {
		super();
		this.observable = observable;
		this.observable.addObserver(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		if(o instanceof ObserverSubject){
			   System.out.println("I am ReceverTwo,I have receiver:"+arg.toString());
		}
     
	}

}
发送一个消息測试这个观察者模式:
public static void main(String[] args) {
		// TODO Auto-generated method stub
        ObserverSubject observer=new ObserverSubject();
        new ReceverOne(observer);
        new ReceverTwo(observer);
        observer.sendNotify("Hello ,this is the first notifition");
        
 
	}
以上代码执行的结果为:I am ReceverTwo,I have receiver:Hello ,this is the first notifition

I am ReceverOne,I have receiver:Hello ,this is the first notifition

看结果可知:编程时不要依赖观察者被通知的顺序。接下来所介绍的是自己定义观察者模式:

先定义要成为观察者的接口,这是主题接口,对象使用此接口注冊为观察者,或者从观察者中移除(这个是成为观察者的条件)

public interface DefineObservAble {

	void registerObserver(DefineObserver observer);
	void removeObserver(DefineObserver observer);
	void notifyObserver(Object o);
}
再定义观察者的接口,全部潜在的观察者,都必须实现这个接口----观察者接口。有通知时调用uPdate方法(这个是观察者的义务)
public interface DefineObserver {

	void update(Object o);
}

然后再创建一个详细的观察者

public class MyObservAble implements DefineObservAble {

	private ArrayList observers=null;
	public MyObservAble(){
		observers=new ArrayList<>();
	}
	@Override
	public  void registerObserver(DefineObserver observer) {
		// TODO Auto-generated method stub
		if(observers==null)return;
		 synchronized(observers){
			int i=observers.indexOf(observer);
			if(i==-1){
				observers.add(observer);
			}
		}
		

	}

	@Override
	public void removeObserver(DefineObserver observer) {
		// TODO Auto-generated method stub
		if(observers==null)return;
		synchronized (observer) {
			int i=observers.indexOf(observer);
			if(i!=-1){
				observers.remove(i);
			}
		}
		

	}

	@Override
	public void notifyObserver(Object o) {
		// TODO Auto-generated method stub
		if(observers==null)return;
		int size=observers.size();
		for(int i=0;i<size;i++){
			DefineObserver observer=(DefineObserver) observers.get(i);
			observer.update(o);
		}
		

	}

	public void sendNotify(String content){
		notifyObserver(content);
	}
}
创建两个能够用于观察的类:
public class ObserverOne implements DefineObserver {

 
	public ObserverOne(DefineObservAble able) {
		able.registerObserver(this);
	}

	@Override
	public void update(Object o) {
		// TODO Auto-generated method stub
       System.out.println("This is the DefineObserver,I am ObserverOne, I have receive:"+o);
	}

}
public class ObserverTwo implements DefineObserver {

 
	public ObserverTwo(DefineObservAble able) {
		able.registerObserver(this);
	}

	@Override
	public void update(Object o) {
		// TODO Auto-generated method stub
       System.out.println("This is the DefineObserver,I am ObserverTwo, I have receive:"+o);
	}

}

最后进行測试,发送一个通知:

/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
   MyObservAble able=new MyObservAble();
   new ObserverOne(able);
   new ObserverTwo(able);
   able.sendNotify(" hello world!");
	}
哎,这个写的太乱啦!概念上不是太清楚。有书上直接写为 主题(注冊,移除,通知更新的类)和观察者(实当前update方法)分别。代码或清除