Difference between Observer Pattern and Publish/Subscribe Pattern

        In the architectural design of the event bus (EventBus), the publish/subscribe mode is used, but it is found that it is very close to the observer mode, and sometimes it is easy to be confused. Now I try to distinguish their relationship.

 

        The roles of the observer mode are the observer (observer) and the subject (subject) object. When the observer needs to observe the subject, it must first register in the subject (the subject object holds the collection handle of the observer object), and then when the internal object of the subject object When the state changes, notify all observers of the change. 

 

        The roles of the publish and subscribe mode are publisher and subscriber. There is no direct coupling relationship between pub and sub. pub publishes a message event (event), sub subscribes to interested message events, and sub can also unsubscribe.

 

        Preliminary conclusions can be drawn:

        Observers in the Observer pattern are tightly coupled to the subject object.

        Publishers and subscribers in the publish/subscribe model are loosely coupled, and publishers and subscribers are connected through events.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326306721&siteId=291194637