Head First设计模式二-----观察者模式

2 Observer Pattern
Publishers + Subscribers = Observer Pattern
publisher = subject
subscribers订阅者 = observers

书中以报社和订报者来形容,真是恰到好处(也有注册,删除,通知等功能)!

定义:定义了对象间的一对多关系,当对象有改变时,所有依赖于它的对象都会得到通知并且自动更新。

Observer Pattern 令主题对象和观察对象松散耦合
因为:
1、主题对象只知道观察者实现了一个特定的接口Observer interface,它不需要知道观察者会干什么,只需要调用接口方法update.
2、任何时间都可以加入新的观察者。
3、不需要改变主题对象来适应新的观察者,只需要新的观察者实现观察者接口就可以加入。

设计原则:在需要交互的对象之间建立松散耦合(Strive for loosely coupled designs between objects that interact)

首先是主题接口(也即被观察者接口),其中定义了注册,删除,通知观察者等方法:
package headfirst.observer.weather;

public interface Subject {
	public void registerObserver(Observer o);
	public void removeObserver(Observer o);
	public void notifyObservers();
}


然后是被观察者的实现

package com.jie.observer;   
  
import java.util.*;   
  
public class WeatherData implements Subject{   
  
    private ArrayList observers;   
    private float temperature;   
    private float humidity;   
    private float pressure;   
       
    public WeatherData()   
    {   
        observers=new ArrayList();   
    }   
       
    public void registerObserver(Observer o)   
    {   
        observers.add(o);   
    }   
       
    public void removeObserver(Observer o)   
    {   
        int i=observers.indexOf(o);   
        if(i>=0)   
        {   
            observers.remove(i);   
        }   
    }   
       
    public void notifyObservers()   
    {   
        for(int i=0;i<observers.size();i++)   
        {   
            Observer observer=(Observer)observers.get(i);   
            observer.update(temperature,humidity,pressure);   
        }   
    }   
       
    public void measurementsChanged()   
    {   
        notifyObservers();   
    }   
       
    public void setMeasurements(float temperature,float humidity,float pressure)   
    {   
        this.temperature=temperature;   
        this.humidity=humidity;   
        this.pressure=pressure;   
        measurementsChanged();   
           
    }   
}  


跟着就是观察者接口:
package com.jie.observer;   
  
ublic interface Observer {   
  
public void update(float temperature,float humidity,float pressure);   


接着当然观察者(实现了上面的观察者接口):
package com.jie.observer;   
  
public class CurrentConditionsDisplay implements Observer,DisplayElement{   
       
    private float temperature;   
    private float humidity;   
    private float pressure;   
    private Subject weatherData;   
       
    public CurrentConditionsDisplay(Subject weatherData)   
    {   
        this.weatherData=weatherData;   
        weatherData.registerObserver(this);   
    }   
       
    public void update(float temperature,float humidity,float pressure)   
    {   
        this.temperature=temperature;   
        this.humidity=humidity;   
        this.pressure=pressure;   
        display();   
    }   
       
    public void display()   
    {   
        System.out.println("Current conditions:"+temperature+"F degrees and "+   
                humidity+"% humidity "+pressure+" pressure");   
    }   
}  


最后是测试类:

package com.jie.observer;   
  
public class Test {   
  
    public static void  main(String [] args)   
    {   
        WeatherData wd=new WeatherData();   
        CurrentConditionsDisplay ccd=new CurrentConditionsDisplay(wd);   
        wd.setMeasurements(21, 13, 30.4f);   
        wd.setMeasurements(1, 2, 3f);   
           
    }   
}  

你可能感兴趣的:(设计模式,F#)