1. Overview
The observer pattern is also called publish / subscribe pattern
The Observer design pattern involves two roles: Subject and Observer
(1) Subject module
Subjec module has three main operations
- addObserver(): register to add observer (apply for subscription)
- deleteObserver(): delete observer (unsubscribe)
- notifyObserver(): notify all observer objects when the subject state changes
(2) Oserver module
The Oserver module has a core operation update(). When the Subject status changes, the update() method of each observer will be called to update the notification.
(3) UML diagram
https://www.processon.com UML diagram drawn Online
2. Simple example
(1) Subject interface
package observer;
public interface Subject {
//Add observer
void addObserver(Observer obj);
//Remove observer
void deleteObserver(Observer obj);
//When the subject method changes, this method is called to notify all observers
void notifyObserver();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
(2) Observer interface
package observer;
public interface Observer {
//Update notification when subject status changes
public void update(int version);
}
- 1
- 2
- 3
- 4
- 5
- 6
(3) Theme implementation class, XX magazine
package observer;
import java.util.ArrayList;
import java.util.List;
public class MagazineSubject implements Subject{
//Store subscribers
private List<Observer> observers=new ArrayList<Observer>();
//Journal version
private int version;
@Override
public void addObserver(Observer obj) {
observers.add(obj);
}
@Override
public void deleteObserver(Observer obj) {
int i = observers.indexOf(obj);
if(i>=0){
observers.remove(obj);
}
}
@Override
public void notifyObserver() {
for(int i=0;i<observers.size();i++){
Observer o=(Observer)observers.get(i);
o.update(version);
}
}
//The magazine has released a new edition
public void publish(){
//New version
this.version++;
//Notify all observers when the information is updated
notifyObserver();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
(4) Observer implementation class
package observer;
public class CustomerObserver implements Observer{
//Subscriber name
private String name;
private int version;
public CustomerObserver(String name){
this.name = name;
}
@Override
public void update(int version) {
this.version=version;
System.out.println("The magazine has a new edition");
this.buy();
}
public void buy(){
System.out.println(name+"Bought the first"+version+"Issue magazine!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
(5) Test class
package observer;
public class Main{
public static void main(String[] args) {
//Create theme (observed)
MagazineSubject magazine = new MagazineSubject();
//Create three different observers
CustomerObserver a = new CustomerObserver("A");
CustomerObserver b = new CustomerObserver("B");
CustomerObserver c = new CustomerObserver("C");
//Register observers with the topic
magazine.addObserver(a);
magazine.addObserver(b);
magazine.addObserver(c);
//Update the subject data. When the data is updated, all registered observers will be notified automatically
magazine.publish();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
results of enforcement
The magazine has a new edition A bought the first issue of the magazine! The magazine has a new edition B bought the first issue of the magazine! The magazine has a new edition C bought the first issue of the magazine!
- 1
- 2
- 3
- 4
- 5
- 6
3. Observer model in big data operation and maintenance platform
Reprinted from: https://blog.csdn.net/chengyuqiang/article/details/79222294