Java设计模式之观察者模式

本文介绍了一种常用的设计模式——观察者模式,通过具体的代码实现展示了如何定义Subject和Observer接口,并给出了一个天气数据更新通知的例子,解释了如何在Subject状态改变时通知所有已注册的Observer进行相应的更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


观察者模式(Observer)


package com.hycz.design.pattern.observer.interfaces;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 3:47 PM
 * To change this template use File | Settings | File Templates.
 */
public interface Subject {

    public void registerObserver(Observer observer);
    public void removeObserver(Observer observer);
    public void notifyObservers();


}

package com.hycz.design.pattern.observer.interfaces;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 3:48 PM
 * To change this template use File | Settings | File Templates.
 */
public interface Observer {

    public void update(float temp, float humidity, float pressure);
}


package com.hycz.design.pattern.observer.interfaces;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 3:48 PM
 * To change this template use File | Settings | File Templates.
 */
public interface DisplayElement {

    public void display();
}

package com.hycz.design.pattern.observer.interfaces.impl;

import com.hycz.design.pattern.observer.interfaces.Observer;
import com.hycz.design.pattern.observer.interfaces.Subject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 3:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class WeatherData implements Subject {

    private List<Observer> subjectList ;
    private float tempreature;
    private float humidity;
    private float pressure;

    public WeatherData(){
        subjectList = new ArrayList<Observer>();
    }

    /**
     * 在 concreteSubject WeatherData 中添加一个subjectList 来保存所有的观察者对象。
     * @param subjectList
     */
    public WeatherData(List<Observer> subjectList) {
        this.subjectList = subjectList;
    }

    /**
     * 把注册的观察者对象保存到subjectList 数组中
     * @param observer
     */
    @Override
    public void registerObserver(Observer observer) {
        subjectList.add(observer);
    }

    /**
     * 移除观察者对象
     * @param observer
     */
    @Override
    public void removeObserver(Observer observer) {
        int index = subjectList.indexOf(observer);
        if (index > 0) subjectList.remove(observer);
    }

    /**
     * 当有数据更新时,通知所有的观察者对象去更新数据
     */
    @Override
    public void notifyObservers() {
        for (Observer item : subjectList){
            item.update(tempreature, humidity, pressure);
        }
    }

    public void measurementsChanged() {
        notifyObservers();
    }

    public void setMeasurements(float tempreature, float humidity, float pressure){
        this.tempreature = tempreature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    // WeatherData 其他方法
}

package com.hycz.design.pattern.observer.interfaces.impl;

import com.hycz.design.pattern.observer.interfaces.DisplayElement;
import com.hycz.design.pattern.observer.interfaces.Observer;
import com.hycz.design.pattern.observer.interfaces.Subject;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 4:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {

    private float humidity;
    private float tempreature;
    private Subject weatherData;

    /**
     * 构造器需要WeatherData(也就是Subject对象),作为注册之用。
     * @param weatherData
     */
    public CurrentConditionsDisplay(Subject weatherData){
        this.weatherData = weatherData;
        this.weatherData.registerObserver(this);
    }

    /**
     * 显示最近的温度和湿度。
     */
    @Override
    public void display() {
        System.out.println("Current Condition = " + this.tempreature
        + "F degress AND " + this.humidity + "% humidity");
    }

    /**
     * 当update方法调用时,把tempreature 和 humidity保存起来,调用display()方法。
     * @param temp
     * @param humidity
     * @param pressure
     */
    @Override
    public void update(float temp, float humidity, float pressure) {
        this.tempreature = temp;
        this.humidity = humidity;
        display();
    }


}

package com.hycz.design.pattern.observer.main;

import com.hycz.design.pattern.observer.interfaces.impl.CurrentConditionsDisplay;
import com.hycz.design.pattern.observer.interfaces.impl.WeatherData;

/**
 * Created with IntelliJ IDEA.
 * User: shangke
 * Date: 6/13/13
 * Time: 4:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class WeatherStation {

    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();
        CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

        weatherData.setMeasurements(80, 65, 34.5f);
    }

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值