“This is the second day of my participation in the First Challenge 2022, for more details: First Challenge 2022”.
Above we explained the event notification mechanism, in this article let’s go straight to the huanglong, see the implementation of the observer mode!
Observer model
Whether it’s an event notification mechanism or an event listening-subscribe implementation, the underlying implementation relies on the observer pattern. Don’t be intimidated by this, and don’t think that design patterns are too obscure to be conquered. Today I’m going to introduce you to the observer model with a simple story.
The story is like this, above we only said the action of Sun Wukong knocking on the ground, but do you still remember that Sun Wukong pointed the golden stick to the sky, and then in return for the thunder male, electric female, dragon king and so on to cast the dharma cloth rain? When I close my eyes, I can still see the competition with Tiger Dasen.
It can be seen that not only the land can receive sun Wukong’s notice, but also the thunder Lord and the dragon King can receive. Here, we compare Sun Wukong to the Subject, that is, the concept of being observed and Subject, and compare the thunder, the lightning, the dragon king and the land to the observer.
Here is our code logic:
First, we define a base class for a topic that keeps a list of all observers subscribed to the topic, and includes methods for adding, deleting, and notifying observers.
public class Subject {
// List of observers
private Vector<Observer> vector = new Vector();
/** * add observer **/
public void addObserver(Observer observer){
vector.add(observer);
}
/** * delete observer **/
public void deleteObserver(Observer observer){
vector.remove(observer);
}
/** * Notify all observers **/
public void notifyObserver(String goldenCudgel) {
for(Observer observer : vector) { observer.update(goldenCudgel); }}}Copy the code
Then, we define an interface for the observer that contains the “actions” the observer takes when it receives a notification.
public interface Observer {
void update(String goldenCudgel);
}
Copy the code
At this time, we define the observer entities of “land”, “thunder and lightning” and “Dragon King” respectively to achieve specific actions such as thunder and rain.
The implementation of “Thunder and lightning” and “Dragon King” is similar to “land”, so only the observer “land” is displayed here.
@Component
public class MyGuardianObserver implements Observer {
@Override
public void update(String goldenCudgel) {
if(upGoldenCudgel(goldenCudgel)) {
System.out.println("Father Land appears."); }}public boolean upGoldenCudgel(String goldenCudgel){
if(Objects.equals(goldenCudgel,"down")) {return true;
}
return false; }}Copy the code
Then, we can define the observed concrete implementation class “Sun Wukong”
public class MonkeyKingSubject extends Subject{
Is the staff up or down? Haha, guess what... * * /
public void doGoldenCudgel(String goldenCudgel){ notifyObserver(goldenCudgel); }}Copy the code
Finally, let’s do a test to see if they can respond to sun Wukong’s message.
@PostMapping
public void observerTest(a){
MonkeyKingSubject subject = new MonkeyKingSubject();
subject.addObserver(new ThunderGodObserver());
subject.addObserver(new MyGuardianObserver());
subject.addObserver(new DragonKingObserver());
subject.doGoldenCudgel("up");
System.out.println("I am the dividing line -----------------------------");
subject.doGoldenCudgel("down");
}
Copy the code
The results show
Tripterygium wilfordii dianmu thunder and lightning The dragon king came to rain I am a line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- land father-in-law appearedCopy the code
conclusion
How can there be no summary at the end of the story? The observer mode and event notification mechanism are both in one-to-many relationship. When an object is modified, it will automatically notify the dependent objects. They are independent and decoupled from each other, which not only saves the resource consumption of repeated state retrieval, but also achieves the highest feedback speed.
Of course, its disadvantages should not be ignored:
- If an observed object has many direct and indirect observers, it can take a long time to notify all observers;
- If there is a circular dependency between the observer and the observing target, the observing target will trigger a circular call between them, possibly causing the system to crash.
- The observer mode has no corresponding mechanism to let the observer know how the observed object changes, but only to know that the observed object changes.
At the end of the article, the source code is provided as usual, and the background reply event can be obtained. The above is all the content of today, if you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!