I. DefinitionThe observer pattern defines a one-to-many dependency relationship, allowing multiple observer objects to monitor a subject object (notifier) at the same time. When the subject object observes changes in the observed object, it notifies all observer objects so that they can update themselves. There are several roles involved here and their own functions:
2. Usage scenariosWhen a change to one object requires changes to other objects at the same time, and there is no need to know how many objects need to be changed 3. Give an exampleIf the dry concept description is obscure and difficult to understand, give an example from common life to explain it Scenario 1: In the office building, in front of the computer. A bunch of programmers took advantage of their boss being away on a business trip to watch NBA games on their computers, shouting excitedly from time to time. At this moment, the boss came back from a business trip and happened to see them, creating an awkward situation. Solution: In order to avoid being caught by the boss who came in when they were slacking off in the company, a few people came up with a plan to bribe the lady at the front desk. When the boss came into the company again, the girl immediately notified the programmers and asked them to return to work. Scenario 2: In the office building, in front of the computer. A bunch of programmers took advantage of their boss being away on a business trip to watch NBA games on their computers, shouting excitedly from time to time. At this time, the boss came back from a business trip. When the lady at the front desk saw the boss coming back, she immediately notified the guys watching the game. At this time, the young men quickly switched to work mode. Scenario 2 uses the observer pattern. When the boss comes back, the programmers need to change their paddling status, and the front desk lady is responsible for notifying them.
| ||||||||||||
<<: How to use bind to set up DNS server
>>: Solution to the problem of eight hours difference in MySQL insertion time
Table of contents 1. beforeCreate & created 2...
Table of contents Why use websocket Socket.io Ope...
This article mainly introduces the dynamic SQL st...
After the previous two chapters, do you have a ne...
Achieve results Code html <div class="css...
1. Create a new object using the Object.create() ...
The effect to be achieved is: fixed zoom in twice...
In the process of database operation, it is inevi...
I believe everyone is very sensitive to colors. C...
Solution 1: Use conditional import in HTML docume...
Table of contents Symbol Data Type The reason why...
Definition and Usage The display property specifi...
Table of contents Overview Solution 1: Closures S...
This article shares the specific code for JavaScr...
Table of contents The node version does not corre...