Example of high temperature early warning function of Meteorological Bureau using observer mode in Java

This paper describes how Java uses observer mode to realize the high temperature early warning function of the Meteorological Bureau. Share with you for your reference, as follows:

Model definition

Observer mode, also known as publish / subscribe mode. Observer pattern defines a one to many dependency between objects. When the state of an object changes, all objects that depend on it are notified and updated automatically.

Two. Example of model

1 mode analysis

The first mock exam is used to illustrate this pattern.

2 observer mode static class diagram

3 code example

3.1 observer interface - IObserver

3.2 subject interface - isubject

3.3 implementation of specific subject

3.4 personal Observer - personobserver

3.5 government observer

3.6 company observer of enterprises and institutions

3.7 let the system run for a period of time

4 operation results

3、 Design principle of this mode

1 "open close" principle 2 single responsibility principle 3 Dependence Inversion Principle

4、 Use occasion

1 when an abstract model has two aspects, one of which depends on the other, and these two aspects need to be encapsulated into independent objects, changed and reused independently of each other. 2 when the change of one object in a system needs to change the contents of other objects at the same time, but we don't know how many objects to change. 3 when the change of an object must notify other objects to make corresponding changes, but it is uncertain who the notification object is.

5、 Static class diagram of "push data"

The so-called "push data" means that when the observed object changes, the relevant data is passed to the observer in the form of parameters, which forms the "push data" of the observed object to the observer. The static class diagram is as follows:

6、 Static class diagram of "pull data"

In the so-called "pull data", the observer object contains a reference to the observed object instance. When the observed object changes, no data will be transmitted to the observer, but the observer actively obtains relevant data according to the reference of the observed object instance, which forms the observer actively "pull data" from the observed object. The static class diagram is as follows:

For more Java related content, interested readers can view the special topics of this site: introduction and advanced tutorial of java object-oriented programming, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>