The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. **Publishers + Subscribers = Observer Pattern** The publisher is called the **subject** and the subscribers are called **observers** in the observer pattern. ![[Screenshot 2023-09-04 at 12.39.02 AM.png]] ![[Screenshot 2023-09-01 at 7.45.55 PM.png]] There are different ways to implement the Observer Pattern, but most resolve around a class design that includes Subject and Observer interfaces ### Lose Coupling: Strive for loosely coupled design between objects that interact The observer pattern is a great example of loose coupling. * The only thing that the subject knows about an observer is that it implements a certain interface (the observer interface). It does not need to know the concrete class of the observer, what it does, or anything else about it. * We can add a new observer at any time. * We never need to modify the subject to add new type of observers. * We can reuse subjects and observers independently of each other. * Changes to either the subject or an observer will not affect the other. ### Should subject push data changes to observers or observers pull the information they need. Whether we pull or push the data to the Observer is an implementation detail, but in a lot of cases it makes sense to let the Observers retrieve the data they need rather than passing more and more data to them through the update() method. * When observers are allowed to pull data, they can use getter methods available in the subject to only get the data they need, rather than all the data.