课程: Node.js: Design Patterns

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

The Observer pattern

The Observer pattern

- [Instructor] The most common analogy for the observer design pattern is a newspaper subscription. Households can subscribe to particular newspapers. When a new paper is printed the news organization delivers that paper to all of its subscribers. The observer design pattern implements a similar approach. Objects, called observers, can watch other objects for state changes. These objects that can broadcast changes are known as observables. In this example, a stoplight is an observable object. Other objects can subscribe to the stoplight and watch it for state changes. For example, a bike, a car, a bus. They're all different types of objects, but they can all watch the stoplight object for changes and respond accordingly. When the state of the stoplight changes, say from red to green, it can notify every object that has subscribed to it of this state change. The key is that each observer object implements a method that…

内容