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
Docker is an open source container engine that he...
This article shares the specific code of JS+AJAX ...
1. Service method Check the firewall status: [roo...
You may often see some HTML with data attributes. ...
Table of contents Node connects to Mysql Install ...
Since the network requests initiated by native js...
Phenomenon The system could compile the Linux sys...
JavaScript writes a random roll call webpage for ...
When you send a network request, the following sa...
Table of contents Preface keep-avlive hook functi...
Table of contents 1. Task Queue 2. To explain som...
Table of contents The origin of JSBridge The bidi...
When Docker creates a container, it uses the brid...
Table of contents 1. Is setState synchronous? asy...
1. The Linux server configures /etc/hosts.deny to...