The publish/subscribe pattern
We will try to overcome the limitations of previous data binding implementations by using a design pattern. We can choose to apply theĀ observer pattern or publisher/subscriber pattern. Both are intended to manage a scenario where one or more objects are interested in receiving notifications when the state of another object changes. The solutions proposed by the patterns are similar and often the two patterns are confused or considered as two different names for the same pattern. Actually, there are important differences, as we will see in the following sections.
The observer pattern
The observer pattern is maybe widely known because it is included in the patterns proposed by the Gang of Four. In its simplified version for JavaScript, that is, not considering class abstractions, it involves the following actors:
Subject: This is the object that may change its state; it knows its observer and sends them a notification when its state changes.
Observers: These are...