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
Preface The reduce() method receives a function a...
1. Rendering 2. Source code HTML < body > &...
Directory Structure . │ .env │ docker-compose.yml...
Preface If you use the overflow: scroll attribute...
Table of contents What is the Linux system that w...
This article example shares the specific code of ...
Preface: This article only introduces the steps t...
Linux version: CentOS 7 [root@azfdbdfsdf230lqdg1b...
background Basic Concepts CSS filter property app...
Table of contents Causes of MySQL Table Fragmenta...
Preface With the crazy speculation of virtual cur...
Putting aside databases, what is dialect in life?...
There are two files a.htm and b.htm. In the same d...
Steps: 1. Install MySQL database 1. Download the ...
Introduction Memcached is a distributed caching s...