Today, I learned the observer pattern and made a summary to make it easier to recall later.
The first is the definition: The Observer pattern is to define a one-to-many dependency between objects, and when an object state changes, all dependent objects receive push messages and make changes on their own initiative.
My understanding: There are a lot of such examples in life, such as meteorological observation station and meteorological display of the relationship between the meteorological display station is the data center, responsible for obtaining the latest meteorological information, and the meteorological display, even if the news shows that all meteorological display instruments rely on weather stations, as well as job seekers and headhunting company relations, Headhunters assume that new information will also be sent to job seekers; Here the weather station is the subject (center), and the viewer is the observer.
The personal feeling assumes that the observer pattern must be used to design the interface-oriented programming principles, to define the subject interface to register, cancel, push, define the Observer interface to update, assume that the observer has other functions, but also to write other interfaces, such as display () interface.
The first is the subject interface: Provides a way to register, cancel, and push messages.
Package Interfaces;public interface Subject {public void Registeobserver (Observer Observer);p ublic void Removeobserver ( Observer Observer);//notify: notice, announcement public void Notifyobserver ();
Then there is the Observer interface, which provides the update operation to update the acquired data to the local instance data.
Package Interfaces;public interface Observer {public void uodate (float temp, float humidity, float pressure);}
Then there is the Displayelement interface, which provides the display method.
Package Interfaces;public interface Displayelement {public void display ();}
Then is the weatherdata, the Weather Information data Center class, implements the subject interface, and provides the Setmeasurements method and measurmentchanged method, the observer register implementation actually is adds this observer to the central list, The notification operation is to iterate through the list, calling the Update method inside.
Package Subjects;import Java.util.arraylist;import Java.util.list;import interfaces. Observer;import interfaces. Subject;public class Weatherdata implements Subject {private List list;private float temperature;private float humidity;p rivate float pressure;public weatherdata () {list = new ArrayList ();} @Overridepublic void Registeobserver (Observer Observer) {list.add (Observer);} @Overridepublic void Removeobserver (Observer Observer) {int i = List.indexof (Observer), if (i>=0) {list.remove (i);}} @Overridepublic void Notifyobserver () {for (int i=0;i<list.size (); i++) {Observer o = (Observer) list.get (i); O.uodate ( temperature, humidity, pressure);}} public void measurementschanged () {notifyobserver ();} public void Setmeasures (float temp, float humidity, float pressure) {this.temperature = Temp;this.humidity = Humidity;this . Pressure = pressure;measurementschanged ();}}
Then we define an observer, Currentconditiondisplay. When you're new, you pass a data center.
Package Observers;import subjects. Weatherdata;import interfaces. Displayelement;import interfaces. Observer;import interfaces. Subject;public class Currentconditiondisplay implements Observer, displayelement {private float tempurature;private Float humidity;private float pressure;private Subject weatherdata;public currentconditiondisplay (weatherdata Weatherdata) {This.weatherdata = Weatherdata;weatherdata.registeobserver (this);} @Overridepublic void Uodate (float tempurature, float humidity, float pressure) {this.tempurature = Tempurature; this.humidity = Humidity;this.pressure = Pressure;display ();} @Overridepublic void Display () {System.out.println (tempurature + ";/n" + humidity + ";/n" +pressure);}}
Finally, a test class:
Package Test;import observers. Currentconditiondisplay;import subjects. Weatherdata;public class Test {public static void main (String args[]) {weatherdata weatherdata = new Weatherdata (); Currentconditiondisplay currentconditiondisplay = new Currentconditiondisplay (weatherdata); weatherData.setMeasures (14.1f, 11.0f, 111.0f);}}
In fact, the Java swing mechanism has a lot of use of such patterns, such as button and monitor relationship, Button is the Observer, the Monitor is the observer, when the AddListener is actually put the monitor object in its own list, The Performxx method of the monitor inside the list is called when you click.
Although only through the sample to understand and practice, but can be in the actual project in the future to think of the use, proficiency and so on, any heavy and long way.
Observer pattern (Observer Patterns)