观察者模式

日常生活中,当我们订阅了报纸后,每天早上报社都会把报纸送到家门口;取消订阅后,报社将不再给我们提供送报纸的服务了;我们把这样的场景可以理解为观察者模式:

1. 观察者(Obersver),享受服务者,它可以订阅多个服务(主题);当主题发生变化时,会自动给观察者发送消息;

2. 主题(Subject),服务提供者,可以为多个观察者提供服务;接受观察者的订阅与取消订阅操作;并同时为订阅者提供服务;

 

优点:

主题与观察者之间高度解耦,是多对多的关系;增加主题或是观察者都对已有的代码没有影响。

 

观察者订阅主题的方式:

observer.register(subject);

观察者删除主题的方式:

observer.remove(subject);

 

 

 

参考代码(JAVA版)

public interface Observer {
    public void update(float temp, float humidity, float pressure);
    
    public void register(Subject sub);
    
    public void remove(Subject sub);
}
public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObserver();
    public void updateData(float temp, float huidity, float pressure);
}    
public class WeatherObserver implements Observer{
    
    private float temp;
    private float humidity;
    private float pressure;
    
    
    @Override
    public void update(float temp, float humidity, float pressure) {
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        
        System.out.println(this.toString());
    }

    public WeatherObserver(){
    }

    
    @Override
    public String toString() {
        return "WeatherObserver [temp=" + temp + ", humidity=" + humidity + ", pressure=" + pressure + "]";
    }

    @Override
    public void register(Subject sub) {
        sub.registerObserver(this);
    }

    @Override
    public void remove(Subject sub) {
        sub.removeObserver(this);
    }
    
    
}
import java.util.LinkedList;
import java.util.List;

public class WeatherSubject implements Subject {

    private List<Observer> observerList;
    private float temp;
    private float humidity;
    private float pressure;
    
    public WeatherSubject(){
        this.observerList = new LinkedList<Observer>();
    }
    @Override
    public void registerObserver(Observer o) {
        observerList.add(o);
        
    }

    @Override
    public void removeObserver(Observer o) {
        if( observerList.contains(o)){
            observerList.remove(o);
        }
        
    }

    @Override
    public void notifyObserver() {
        int size = observerList.size();
        for(int i = 0; i < size; i++){
            observerList.get(i).update(temp, humidity, pressure);
        }
        
    }

    public void updateData(float temp, float huidity, float pressure){
        this.temp = temp;
        this.humidity = huidity;
        this.pressure = pressure;
    }
}
public class Main {

    public static void main(String[] args) {

        Subject weatherSubject = new WeatherSubject();
        
        Observer weatherObs = new WeatherObserver();

        weatherObs.register(weatherSubject);
        
        weatherSubject.updateData(10, 20, 30);
        weatherSubject.notifyObserver();
        
        weatherObs.remove(weatherSubject);
        weatherSubject.updateData(10, 20, 30);
        weatherSubject.notifyObserver();
        
        weatherSubject.updateData(10, 20, 30);
        weatherSubject.notifyObserver();
    }

}

 

posted @ 2017-03-10 17:56  ordi  阅读(190)  评论(0编辑  收藏  举报