观察者模式 vs 订阅发布模式

一直很好奇订阅/发布模式和观察者模式有什么区别,一直以来都把这两者混为一谈。

实际上,我理解中的观察者模式是1:n,而订阅发布模式是m:n的模式。观察者模式需要观察者直接在被观察这种注册,两者直接耦合,一般是同步的,而订阅发布模式则存在一个调度中心,观察者与被观察者不关心对方是谁,只通过消息通信,一般是异步的。

观察者模式

image.png
class DownloadTask {
  private status: 'loadding' | 'finished' | 'pause' = 'loadding';

  constructor(public id: number, public taskName: string) {}

  public finish() {
    this.status = 'finished';
    console.log(`Task ${this.taskName} is notifed`);
  }

  public pause() {
    this.status = 'pause';
  }

  public start() {
    this.status = 'loadding';
  }
}
class DownloadList {
  public downloadList: DownloadTask[];
  constructor() {
    this.downloadList = [];
  }

  public add(task: DownloadTask) {
    this.downloadList.push(task);
  }

  public remove(id: number) {
    for (let task of this.downloadList) {
      if (task.id === id) {
        const index = this.downloadList.indexOf(task);
        this.downloadList.splice(index, 1);
      }
    }
  }

  public get(index: number) {
    return this.downloadList[index];
  }

  public length() {
    return this.downloadList.length;
  }
}
class DataHub {
  public downloadList = new DownloadList();

  public addDownloadTask(task: DownloadTask) {
    this.downloadList.add(task);
  }

  public removeDownloadTask(task: DownloadTask) {
    this.downloadList.remove(task.id);
  }

  public notify() {
    const len = this.downloadList.length();

    for (let i = 0; i < len; i++) {
      this.downloadList.get(i).finish();
    }
  }
}


const task1 = new DownloadTask(1, 'task1');
const task2 = new DownloadTask(2, 'task2');

const dataHub = new DataHub();

dataHub.addDownloadTask(task1);
dataHub.addDownloadTask(task2);

dataHub.notify();

订阅发布模式

image.png
class DownloadTask {
  private status: 'loadding' | 'finished' | 'pause' = 'loadding';

  constructor(public id: number, public taskName: string) {}

  public finish() {
    this.status = 'finished';
    console.log(`Task ${this.taskName} is notifed`);
  }

  public pause() {
    this.status = 'pause';
  }

  public start() {
    this.status = 'loadding';
  }
}
class DownloadList {
  public downloadList: DownloadTask[];
  constructor() {
    this.downloadList = [];
  }

  public add(task: DownloadTask) {
    this.downloadList.push(task);
  }

  public remove(id: number) {
    for (let task of this.downloadList) {
      if (task.id === id) {
        const index = this.downloadList.indexOf(task);
        this.downloadList.splice(index, 1);
      }
    }
  }

  public get(index: number) {
    return this.downloadList[index];
  }

  public length() {
    return this.downloadList.length;
  }
}
class DataHub {
  public downloadList = new DownloadList();

  public addDownloadTask(task: DownloadTask) {
    this.downloadList.add(task);
  }

  public removeDownloadTask(task: DownloadTask) {
    this.downloadList.remove(task.id);
  }

  public notify() {
    const len = this.downloadList.length();

    for (let i = 0; i < len; i++) {
      this.downloadList.get(i).finish();
    }
  }
}
class DepManager {
  public events: {
    [index: string]: ((...rest: any[]) => void)[];
  } = {};

  public registerChannel(evtName: string) {
    if (!this.events[evtName]) {
      this.events[evtName] = [];
    } else {
      console.error('already registed');
    }
  }

  public subscribe(evtName: string, cb: any) {
    if (this.events[evtName]) {
      this.events[evtName].push(cb);
    }
  }

  public emit(evtName: string, payload?: any) {
    if (this.events[evtName]) {
      try {
        const subscribers = this.events[evtName];
        for (let i = 0; i < subscribers.length; i++) {
          subscribers[i].call(null, payload);
        }
      } catch (e) {}
    }
  }
}
const task1 = new DownloadTask(1, 'task1');
const task2 = new DownloadTask(2, 'task2');

const taskList = new DownloadList();
taskList.add(task1);
taskList.add(task2);

const manager = new DepManager();

const dataHub = new DataHub();

manager.registerChannel('datahub');

manager.subscribe('datahub', () => {
  console.log('dispatch');
});

manager.emit('datahub');

总结

其实比较明显的地方在于观察者模式提供了一种点对多点的交互,但是当业务逻辑复杂的时候这种关系难以维护,因为需要单独对于被观察者进行注册,而订阅发布者模式则提供了一种统一事件管理方式,但相对的,实现所需要的成本则更高一些。

注:
以上的代码源自知乎:https://www.zhihu.com/question/23486749

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,473评论 19 139
  • 更新 谢谢各位朋友帮我想主意。此项目需求解决方案总结如下: tableview或者collection view通...
    Big丶Show阅读 12,842评论 24 35
  • 表叔的女儿要出嫁了,过来祝贺同时探望一下姥妗(潮汕话:父亲的舅妈)。很荣幸作为家里的长孙,和老一辈人有着比较多的缘...
    远见为识阅读 2,586评论 6 1