【设计模式】工厂模式、单例模式、观察者模式、发布订阅模式

devtools/2024/9/23 10:27:56/

1.工厂模式

class Factory{createProduct(name){return new Product(name);}
}
class Product{constructor(name){this.name=name;}display(){console.log(`product:${this.name}`);}
}//使用
const factory=new Factory();
const p1=factory.createProduct('P1');
const p2=factory.createProduct('P2');
p1.display()
p2.display()

2.单例模式

class Singleton{static instance=null;constructor(){if(Singleton.instance){return Singleton.instance}Singleton.instance=this;}
}//使用
const instance1=new Singleton()
const instance2=new Singleton()

3.观察者模式

class Subject{constructor(){this.observers=[];}addObserver(observer){this.observers.push(observer);}removerObserver(observer){this.observers=this.observers.filter(obs=>obs!==observer);}notifyObserver(){this.observers.forEach(obs=>obs.update());}
}class Observer{constructor(name){this.name=name;}update(){console.log(`Observer ${this.name} has been notified`);}
}
//使用
const subject=new Subject();
const observer1=new Observer('1');
const observer2=new Observer('2');
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.notifyObserver();

4.发布订阅模式

class Broker{constructor(){this.subscribers=[];this.state=0;}subscribe(subscriber){this.subscribers.push(subscriber);}setState(state){this.state=state;this.publish();}getState(){return this.state;}publish(){this.subscribers.forEach(sub=>sub.update());}
}class Publisher{constructor(){}changeState(broker,state){broker.setState(state);}
}class Subscriber{constructor(name,broker){this.name=name;this.broker=broker;this.broker.subscribe(this);}update(){console.log(`${this.name}:${this.broker.getState()}`);}
}
//使用
const broker=new Broker();
const publish=new Publisher();
const subscribe1=new Subscriber('s1',broker);
const subscribe2=new Subscriber('s2',broker);
publish.changeState(broker,1);

http://www.ppmy.cn/devtools/115952.html

相关文章

爬虫--翻页tips

免责声明:本文仅做分享! 伪线程 from DrissionPage import ChromiumPage import timepage ChromiumPage() page.get("https://you.ctrip.com/sight/taian746.html") # 初始化 第0页 index_page 0# 翻页点击函数 sleep def page_turn():page…

MySQL13、事务MVCC

什么是事务? 我们知道MySQL是一个客户端服务器架构的软件。对于同一个服务器来说,可以有多个客户端连接,每个客户端与服务器建立连接后,就形成了一个会话。每个客户端都可以在自己的会话中向服务器发出请求语句,一个请…

Android 系统WIFI AP模式

在 Android 系统中,AP 模式(Access Point Mode,热点模式)允许设备作为 Wi-Fi 热点,其他设备可以通过连接这个热点进行互联网访问或局域网通信。要让 Android 设备工作在 AP 模式,你可以通过应用层的 API 控…

武汉大学:如何做好高校电子邮件账号安全防护

上个世纪七十年代,电子邮件占据了互联网的前身ARPANET上流量的75%,是最主要的应用。随着互联网的发展,电子邮件在全面普及后,被各种各样的即时通讯软件抢走了不少风头。然而,其始终还是被社会所认可的主流网络通讯渠道…

2011-2022年数字金融与企业ESG表现:效应、机制与“漂绿”检验(内含原始数据+处理代码)

2011-2022年数字金融与企业ESG表现:效应、机制与“漂绿”检验(内含原始数据处理代码) 1、时间:2011-2022年 2、来源:上市公司年报、华证ESG、北大数字普惠金融 3、指标:年份、股票代码、股票简称、行业名…

基于Jeecgboot3.6.3的flowable流程增加任务节点字段的控制(三)

更多技术支持与服务请加入我的知识星球,名称:亿事达nbcio技术交流社区https://t.zsxq.com/iPi8F 8、下面主要先讲OA流程的VForm3的表单字段控制,把流程节点显示与录入分开 <!--流程各个节点表单加载显示VForm3表单--><div class="test-form" v-for=&quo…

Spark原理及调优

spark官档 hints&#xff1a;https://spark.apache.org/docs/3.0.0/sql-ref-syntax-qry-select-hints.html调优参数&#xff1a;https://spark.apache.org/docs/latest/sql-performance-tuning.html#join-strategy-hints-for-sql-queries作者几乎把所有的RDD API查了个遍&…

linux-----进程控制

提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、fork()函数 返回值&#xff1a;子进程返回0&#xff0c;父进程返回子进程的id,出错就返回-1. fork创建子进程&#xff0c;如果父子一方发生写入时&#xff0c;就会发生写实拷贝&#xff0c;操作系统就…