search

Home  >  Q&A  >  body text

node.js - nodejs的事件回调机制是否属于设计模式中的某一种?

属于观察者模式吗?那么谁是观察者?谁是被观察者?

PHPzPHPz2858 days ago687

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 11:28:49

    This is called reactor mode
    Speaking of asynchronous IO, it is actually difficult to achieve true asynchronous now (unless the potential of the hardware is used for asynchronous triggering). In most cases, it is still necessary to block in a multiplexed function, such as select or epoll, and wait until the descriptor Ready (for example, the network card buffer is readable and writable), and then calls the callback function registered on the corresponding descriptor. This approach is the basic idea of ​​current reactor design. Use Twisted’s picture to explain

    reply
    0
  • 阿神

    阿神2017-04-17 11:28:49

    event-driven is 基于观察者模式’s

    http://www.dofactory.com/javascript/observer-design-pattern

    Event handler is 观察者, event source is 被观察者

    The idea of ​​the observer pattern is: 被观察者Maintain a list of a series of 观察者s, and when the theme changes, 通知all observers
    This notification mechanism is implemented in JavaScript in the form of callback, which is completely in line with the design concept of the observer pattern

    reply
    0
  • 黄舟

    黄舟2017-04-17 11:28:49

    It should be proactor, right?

    reply
    0
  • Cancelreply