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

打印 上一主题 下一主题

主题 689|帖子 689|积分 2067

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.工厂模式
  1. class Factory{
  2.     createProduct(name){
  3.         return new Product(name);
  4.     }
  5. }
  6. class Product{
  7.     constructor(name){
  8.         this.name=name;
  9.     }
  10.     display(){
  11.         console.log(`product:${this.name}`);
  12.     }
  13. }
  14. //使用
  15. const factory=new Factory();
  16. const p1=factory.createProduct('P1');
  17. const p2=factory.createProduct('P2');
  18. p1.display()
  19. p2.display()
复制代码
2.单例模式
  1. class Singleton{
  2.     static instance=null;
  3.     constructor(){
  4.         if(Singleton.instance){
  5.             return Singleton.instance
  6.         }
  7.         Singleton.instance=this;
  8.     }
  9. }
  10. //使用
  11. const instance1=new Singleton()
  12. const instance2=new Singleton()
复制代码
3.观察者模式
  1. class Subject{
  2.     constructor(){
  3.         this.observers=[];
  4.     }
  5.     addObserver(observer){
  6.         this.observers.push(observer);
  7.     }
  8.     removerObserver(observer){
  9.         this.observers=this.observers.filter(obs=>obs!==observer);
  10.     }
  11.     notifyObserver(){
  12.         this.observers.forEach(obs=>obs.update());
  13.     }
  14. }
  15. class Observer{
  16.     constructor(name){
  17.         this.name=name;
  18.     }
  19.     update(){
  20.         console.log(`Observer ${this.name} has been notified`);
  21.     }
  22. }
  23. //使用
  24. const subject=new Subject();
  25. const observer1=new Observer('1');
  26. const observer2=new Observer('2');
  27. subject.addObserver(observer1);
  28. subject.addObserver(observer2);
  29. subject.notifyObserver();
复制代码
4.发布订阅模式
  1. class Broker{
  2.     constructor(){
  3.         this.subscribers=[];
  4.         this.state=0;
  5.     }
  6.     subscribe(subscriber){
  7.         this.subscribers.push(subscriber);
  8.     }
  9.     setState(state){
  10.         this.state=state;
  11.         this.publish();
  12.     }
  13.     getState(){
  14.         return this.state;
  15.     }
  16.     publish(){
  17.         this.subscribers.forEach(sub=>sub.update());
  18.     }
  19. }
  20. class Publisher{
  21.     constructor(){}
  22.     changeState(broker,state){
  23.         broker.setState(state);
  24.     }
  25. }
  26. class Subscriber{
  27.     constructor(name,broker){
  28.         this.name=name;
  29.         this.broker=broker;
  30.         this.broker.subscribe(this);
  31.     }
  32.     update(){
  33.         console.log(`${this.name}:${this.broker.getState()}`);
  34.     }
  35. }
  36. //使用
  37. const broker=new Broker();
  38. const publish=new Publisher();
  39. const subscribe1=new Subscriber('s1',broker);
  40. const subscribe2=new Subscriber('s2',broker);
  41. publish.changeState(broker,1);
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

星球的眼睛

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表