What is the Observer Pattern?
Scenario simulation
Code Implementation//Observer design pattern //Publisher-->Merchant var shopObj = {}; //Product list [key:[]], key is the product name shopObj.list = []; //Subscription method shopObj.listen = function ( key, fn) { // key is the product model, fn is the subscription behavior if (!this.list[key]) { this.list[key] = []; } this.list[key].push(fn); //Add a subscription to the product list with the product name key} //Publish message method shopObj.publish = function (key) { //var key = arguments[0]; //If you don't pass the parameter key, you can also do this var fns = this.list[key]; // for (var i = 0; i < fns.length; i++) { for(var i = 0 ,fn; fn = fns[i++];){ //Execute the subscribed function fn arguemnts to store all arguments // var fn = fns[i++]; fn.apply(this, arguments) } } // User A adds a subscriptionshopObj.listen("Huawei", function (brand, model) { console.log("User A received: " + brand + model + "The price of the mobile phone has been reduced"); }) // User B adds subscription shopObj.listen("Huawei", function (brand, model) { console.log("User B received: " + brand + model + "The price of the mobile phone has been reduced"); }) //c user adds subscriptionshopObj.listen("小米", function (brand, model) { console.log("User C received: " + brand + model + "The price of the mobile phone has been reduced"); }) //Double 11 merchants publish information about Huawei's price cutsshopObj.publish("Huawei", "p30"); shopObj.publish("小米", "Mix4"); Refactoring the code//Observer design pattern var Eevent = { //Product list [key:[]], key is the product name list: [], //Subscription method listen: function (key, fn) { // key is the product model, fn is the subscription behavior if (!this.list[key]) { this.list[key] = []; } this.list[key].push(fn); }, //Publish message method publish: function (key) { //var key = arguments[0]; //If you don't pass the parameter key, you can also do this var fns = this.list[key]; // for (var i = 0; i < fns.length; i++) { for (var i = 0, fn; fn = fns[i++];) { //Execute the subscribed function fn arguemnts to store all arguments // var fn = fns[i++]; fn.apply(this, arguments) } } } //Observer object initialization var initEvent = function (obj) { for (var i in Eevent) { obj[i] = Eevent[i]; } } //Publisher->Merchant var shopObj = {}; initEvent(shopObj); // User A adds a subscriptionshopObj.listen("Huawei", function (brand, model) { console.log("User A received: " + brand + model + "The price of the mobile phone has been reduced"); }) // User B adds subscription shopObj.listen("Huawei", function (brand, model) { console.log("User B received: " + brand + model + "The price of the mobile phone has been reduced"); }) //c user adds subscriptionshopObj.listen("小米", function (brand, model) { console.log("User C received: " + brand + model + "The price of the mobile phone has been reduced"); }) //Double 11 merchants publish information about Huawei's price cutsshopObj.publish("Huawei", "p30"); shopObj.publish("小米", "Mix4"); SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Analysis of the process of publishing and deploying Spring Boot applications through Docker
Table of contents 1. Overview 1.1 Usage of queryS...
1. Install Docker yum install docker #Start the s...
text OK, next it’s time to show the renderings. O...
Overview In actual business scenario applications...
Transactional Characteristics 1. Atomicity: After...
In daily development tasks, we often use MYSQL...
Ubuntu 20.04 does not have root login enabled by ...
<br />In HTML language, you can automaticall...
From the tomcat configuration file, we can see th...
MySQL escape Escape means the original semantics ...
Table of contents Preface Child components pass d...
Detailed explanation and summary of the URL for d...
describe This article introduces a method to impl...
need: Usually, sites want to prevent videos and p...
1.v-bind (abbreviation:) To use data variables de...