Let's take a look at the text in the javascript design mode and development practice book to describe the publishing and subscription mode in reality. James recently looked at a house and was notified of it only after the house was sold...
First, extract the text in the javascript design mode and development practice book // the publishing and subscription mode in reality
James recently saw a house and was told that the house had already been sold out after arriving at the sales office. Fortunately, the sales MM told James that there will be some ending disks launched soon. The developer is going through the relevant procedures and can purchase them after the procedures are completed. However, no one knows when. Then James noted down the phone number at the sales office and called every day to check if the purchase time was reached. In addition to Xiao Ming, Xiao Hong, Xiao Qiang, and Xiao Long will also ask the sales office about the problem every day. One week later, the sales MM decided to resign because he was tired of answering 1000 phone calls with the same content every day.
Of course, there is not such a stupid Sales Company in reality. The story is as follows: James left his phone number at the sales office before leaving. The sales MM agreed to inform James immediately after the launch of the new building. The same is true for Xiaohong, Xiaoqiang, and Xiaolong. their phone numbers are recorded on the roster at the sales office. When new buildings are launched, the sales center MM will open the roster and traverse the phone numbers above, send an SMS to notify them one by one. In the previous example, sending text message notifications is a typical publishing-subscription model. buyers such as James and Tom are subscribers who subscribe to the house sales. As a publisher, the sales office will traverse the phone numbers on the roster at the right time and send messages to buyers in turn.
In this example, the publishing-subscription mode has obvious advantages. Buyers no longer need to call the sales office every day to ask about the sales start time. At the right time, the sales office will serve as a publisher to notify these message subscribers. Buyers and sales offices are no longer strongly coupled. When a new buyer appears, he only needs to leave his mobile phone number in the sales office. The sales office does not care about any situations of buyers, whether the buyers are men, women, or monkeys. Any change in the sales office will not affect the buyers. For example, the sales office MM resigned and the sales office moved from the first floor to the second floor. These changes have nothing to do with the buyers. As long as the sales office remembers to send text messages.
First, you must specify who acts as the publisher (such as the sales office). Then, add a cache list to the publisher to store the callback function to notify the subscriber (the sales office Roster, the publisher will traverse the cache list and trigger the number of subscriber callbacks in turn (traverse the roster and send messages one by one ). In addition, we can also enter some parameters in the callback function, which the subscriber can receive. This is necessary. For example, the sales office can add the unit price, area, floor area ratio, and other information to the text message sent to the subscriber. After receiving the information, the subscriber can process the information separately:
Observer ModeClick meScript // my personal understanding // The publisher first needs an object to save the callback function of the event. The attribute of this object is the event type, for example, {"click": [fn1, fn2], "mouseover": [fn3, fn4]} // subscribe to an event: first, determine whether the event type exists on the current object, if it does not exist, the attribute will be added after initialization, and set the value of this attribute to an empty array // if it already exists, push the function to the corresponding attribute value for a long time, this value is an array // release event: each event is sent to the subscriber, and the events you have published. Here we need to determine if, // first extract the key of the event to be published from the parameter, and then obtain the array of callback functions corresponding to the key, after retrieving the array, first determine whether the array exists or whether the array length is 0. If it does not exist or is 0 //, the program ends. If the preceding conditions are not true, the for loop is executed, traverse the callback function array at a time and execute the functions in it. Here, you need to change this point of the callback function. Prevent loss of // this. // Removal event: first, determine whether the value of the key to be removed is null. If it is null, exit the program. Otherwise, determine whether the callback function to be removed is passed in. If it is null, if you want to remove all functions, set the array length to 0 // The following is the implementation process of js design pattern and development practices var Event = (function () {var clientList ={}, listen, trigger, remove; listen = function (key, fn) {if (! ClientList [key]) {clientList [key] = [];} clientList [key]. push (fn) ;}; trigger = function () {var key = Array. prototype. shift. call (arguments), cb = clientList [key]; if (! Cb | cb. length = 0) {return false; // exit the program if the callback function is not saved.} for (var I = 0; I
The author also explained the disadvantages of the publishing and subscription model:
If too many global publishing-subscription modes are used for communication between modules, the connection between modules is hidden. In the end, we will not know which module the message comes from or which modules the message will flow to. This will cause some trouble for our maintenance, maybe the role of a module is to expose some interfaces to call other modules.
The above is the details of my personal understanding of the subscriber mode released in js. For more information, see other related articles in the first PHP community!