[Project actual combat] Observer mode of design mode, Observer behavior

1. Observer mode

1.1 What is the observer pattern?

Observer mode is a design pattern that defines a one-to-many dependency, allowing multiple observer objects to monitor a subject object at the same time. All observer objects automatically update themselves when the state of the subject object changes.

The Observer pattern defines a multi-team dependency, allowing multiple observer objects to monitor a subject object at the same time. This subject object notifies all observer objects when there is a change in state, enabling them to update themselves automatically.

1.2 The main role of the observer mode

The main roles of the Observer pattern include:

1.2.1 Subject

The observed object, if there is a state change, will notify all observer objects.

1.2.2 Observer object (Observer)

Observer, which subscribes to the state changes of the subject object. When the state of the subject object changes, the observer object will be notified.

1.3 The basic process of the observer mode

The basic process of the observer mode is as follows:

  • The subject object subscribes to the observer object, and the observer object starts to monitor the state changes of the subject object.
  • When the state of the subject object changes, the observer object is notified and updates itself.

2. Examples of use

The following is a simple observer pattern sample code written in Java:

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

interface Subject {
    
    
    void attach(Observer observer);
    void detach(Observer observer);
    void notifyObservers();
}

class WeatherData implements Subject {
    
    
    private List<Observer> observers = new ArrayList<>();
    private String weatherCondition;

    public WeatherData(String weatherCondition) {
    
    
        this.weatherCondition = weatherCondition;
    }

    public void attach(Observer observer) {
    
    
			(weatherCondition);
        }
    }
}

interface Observer {
    
    
    void update(String weatherCondition);
}

class WeatherStation {
    
    
    private Subject weatherData;

    public WeatherStation(Subject weatherData) {
    
    
        this.weatherData = weatherData;
    }

    public void reportWeather(String weatherCondition) {
    
    
        weatherData.notifyObservers();
    }
}

class Radio implements Observer {
    
    
    private WeatherStation weatherStation;

    public Radio(WeatherStation weatherStation) {
    
    
        this

class Television implements Observer {
    
    
    private WeatherStation weatherStation;

    public Television(WeatherStation weatherStation) {
    
    
        this.weatherStation = weatherStation;
        weatherStation.attach(this);
    }

    @Override
    public void update(Sunny");
        WeatherStation weatherStation = new WeatherStation(weatherData);
        Radio radio = new Radio(weatherStation);
         Television television = new Television(weatherStation);
         weatherData.notifyObservers();
    }
}

In this example, the Subject interface defines the subject object in the Observer pattern, and the WeatherData class implements the Subject interface and represents weather data. The WeatherStation class is a specific observer in the observer pattern. It establishes a relationship with the Radio and Television classes, and calls the notifyObservers method of the subject object in its reportWeather method to notify all observers to update the status.

In the main function, we create the WeatherData object, WeatherStation object, Radio object and Television object, and when the notifyObservers method of the WeatherData object is called, the observer object will update its state.

Guess you like

Origin blog.csdn.net/wstever/article/details/129890052