I. Introduction to the observer model
1. What is the observer model
Observer Pattern, also known as publish and subscribe Pattern, belongs to a behavioral design Pattern and is defined as: Define a one-to-many dependency between objects so that when one object changes state, All its dependents are notified and updated automatically (defines a one-to-many dependency between objects so that all dependent objects are notified and updated when a single object changes) The Observer pattern is widely used in projects, and it is not difficult to understand. Simply put, the observer pattern is a solution to solve data or behavior consistency.
2. Service Description
There are too many real-world mapping scenarios for the observer mode. Like a lot of friends when I was a child must have a classmate: this kind of person as long as you have an illegal move, he would immediately to your teacher in reaction to achieve the purpose of Roman holiday, after the teacher in charge to get news from you violate compasses, and will be for you to make punishment, and the chain business is one of the most typical applications in the observer pattern.
Here we will make the business model more specific. Xiao Ming is a naughty boy. Although his academic performance is good, he still likes to be absent in class, for example, when he knows everything the teacher says, he likes to secretly open comic books and read cartoons. And this behavior will sometimes be his deskmate Xiao Hong reaction to the teacher.
So let’s briefly analyze the roles involved in this business model:
- Subject: In this business model, it is Clearly Ming. Your every move is being made by the spy observer sitting next to you.
- Observer: Yes, an Observer is a person who always reports to his teacher.
Next we will do a simple code implementation:
3. Code implementation
- It is preferred to create a student interface
public interface Subject {
void learn(a);
}
Copy the code
- Creating a Spy Interface
public interface Observer {
void update(a);
}
Copy the code
- Create the observed interface
public interface Subject {
void registerObserver(Observer observer);
void deleteObserver(Observer observer);
void notifyObserver(a);
}
Copy the code
- Observer Implementation class (Little Red)
public class Xiaohong implements Student.Observer {
@Override
public void update(a) {
System.out.println("Xiao Hong observed Xiao Ming reading a cartoon and was going to report to the teacher.");
}
@Override
public void learn(a) {
System.out.println("Xiao Hong is concentrating on her studies."); }}Copy the code
- Observed implementation class (Xiaoming)
public class Xiaoming implements Student.Subject {
private List<Observer> observerList = new ArrayList<>();
@Override
public void learn(a) {
System.out.println("Xiao Ming is studying hard.");
}
public void cartoon(a) {
System.out.println("Xiao Ming is reading a cartoon.");
notifyObserver();
}
@Override
public void registerObserver(Observer observer) {
observerList.add(observer);
}
@Override
public void deleteObserver(Observer observer) {
observerList.remove(observer);
}
@Override
public void notifyObserver(a) {
for(Observer observer : observerList) { observer.update(); }}}Copy the code
- The test code
public class Client {
public static void main(String[] args) {
Xiaoming xiaoming = new Xiaoming();
xiaoming.registerObserver(newXiaohong()); xiaoming.cartoon(); }}Copy the code
Test results: At this point a simple observer model is complete.
Second, design ideas
1. Design the class diagram
After completing the code implementation, let’s take a look at one of the basic design structures of the Observer pattern:Here we can see that the observer model is actually mainly composed of two parts:
- Subject: Defines the behavior that the observed must perform. It can dynamically add or remove observers. It must be responsible for managing and informing observers.
- Observer: The Observer is mainly responsible for taking specified actions after receiving the observed message and processing the received information.
2. Characteristics of the observer model
Advantages:
- Good system expansibility
- You can build a simple set of trigger chains
Disadvantages:
- The execution efficiency of multi-level triggering is not optimal
3. Usage scenarios of observer mode
- Associative behavior scenarios, note that associative behavior here refers to detachable rather than combined.
- Multilevel event triggering
- Cross-system information interaction, such as message queue processing mechanisms
4. Observer mode in Java
An Observer interface java.util.Observer and an Observable implementation class java.util.Observable are provided for developers at the beginning of Java design, so that we can directly implement or inherit the interface provided by JDK when implementing the Observer mode.
Three, endnotes
Today’s proxy mode explanation is all over, the related code of the design mode has been included in Gitee, you can take it directly if you need it:
Gitee.com/xiaolong-ob…
If you have any questions, you are welcome to leave a message in the comment section or send a private message to the blogger, who will answer them for you in the first time. The code word is not easy, feel the harvest of friends remember to pay attention to the blogger, do not be white whoring monster oh ~ blogger here wish you can be promoted in the New Year, to the peak of life!