《HeadFirst设计模式(第二版)》第二章代码——观察者模式

代码文件目录结构:

《HeadFirst设计模式(第二版)》第二章代码——观察者模式_第1张图片

DisplayElement
package Chapter2_ObserverPattern.interfaces;

/**
 * @Author 竹心
 * @Date 2023/8/2
 **/

public interface DisplayElement {
    public void display();
}
Observer
package Chapter2_ObserverPattern.interfaces;

/**
 * @Author 竹心
 * @Date 2023/8/2
 **/

public interface Observer {
    //通知观察者数据已经更新
    public void update();
}
Subject
package Chapter2_ObserverPattern.interfaces;

/**
 * @Author 竹心
 * @Date 2023/8/2
 **/

public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObserver();
}
CurrentConditionsDisplay
package Chapter2_ObserverPattern;

import Chapter2_ObserverPattern.interfaces.DisplayElement;
import Chapter2_ObserverPattern.interfaces.Observer;

/**
 * @Author 竹心
 * @Date 2023/8/2
 **/

public class CurrentConditionsDisplay implements Observer, DisplayElement {
    private float temperature;
    private float humidity;
    private WeatherData weatherData;

    //初始化的同时订阅weatherData
     public CurrentConditionsDisplay(WeatherData weatherData){
         this.weatherData = weatherData;
         this.weatherData.registerObserver(this);
     }

    @Override
    public void display() {
        System.out.println("Current conditions: "+
                this.temperature+"F degrees and "+
                this.humidity+"% humidity");
    }

    public void update(){
        //得知数据更新后,观察者根据自己的需要获取数据
         this.temperature = this.weatherData.getTemperature();
         this.humidity = this.weatherData.getHumidity();
         this.display();
     }
}
WeatherData
package Chapter2_ObserverPattern;

import Chapter2_ObserverPattern.interfaces.Observer;
import Chapter2_ObserverPattern.interfaces.Subject;

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

/**
 * @Author 竹心
 * @Date 2023/8/2
 **/

public class WeatherData implements Subject {
    private List observers;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData(){
        this.observers = new ArrayList();
    }

    public float getTemperature() {
        return temperature;
    }

    public float getHumidity() {
        return humidity;
    }

    public float getPressure() {
        return pressure;
    }

    @Override
    public void registerObserver(Observer o) {
        this.observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        this.observers.remove(o);
    }

    @Override
    public void notifyObserver() {
        //通知所有订阅者,数据已经更新
        for(Observer observer:this.observers){
            observer.update();
        }
    }

    public void measurementsChanged(){
        this.notifyObserver();
    }

    //更新气天气信息
    public void setMeasurements(float temperature,float humidity,float pressure){
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        this.measurementsChanged();
    }
}
WeatherStation
package Chapter2_ObserverPattern;

/**
 * @Author 竹心
 * @Date 2023/8/2
 **/

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

        CurrentConditionsDisplay currentDisplay =
                new CurrentConditionsDisplay(weatherData);

        //更新天气信息
        weatherData.setMeasurements(80,65,30.4f);
        weatherData.setMeasurements(82,70,29.2f);
        weatherData.setMeasurements(78,90,29.3f);

    }
}
SwingObserverExample
package Chapter2_ObserverPattern;

import javax.swing.*;

/**
 * @Author 竹心
 * @Date 2023/8/2
 **/

public class SwingObserverExample {
    JFrame frame;

    public static void main(String[] args) {
        SwingObserverExample example = new SwingObserverExample();
        example.go();
    }

    public void go(){

        this.frame = new JFrame();
        this.frame.setSize(300,200);

        JButton button = new JButton("Should I do it?");
        button.addActionListener(event-> System.out.println("Don't do it, you might regret it!"));
        button.addActionListener(event-> System.out.println("Come on, do it!"));

        this.frame.add(button);
        this.frame.setVisible(true);
    }
}

你可能感兴趣的:(设计模式,观察者模式,java)