官术网_书友最值得收藏!

  • jQuery Design Patterns
  • Thodoris Greasidis
  • 451字
  • 2021-07-16 12:52:27

Introducing the Publish/Subscribe Pattern

The Publish/Subscribe Pattern is a Messaging Pattern where the emitters of the messages, called the publishers, multicast messages to a number of recipients, called the subscribers, that have expressed their interest in receiving such messages. The key concept of this pattern, which is also commonly referred to as the Pub/Sub Pattern in short, is to provide a way to avoid dependencies between the publishers and their subscribers.

An extra concept of this pattern is the use of topics that are used by the subscribers in order to express that they are only interested in messages of a specific type. This way, publishers filter subscribers before sending a message and distribute that message only to the appropriate ones, thereby reducing the amount of traffic and work required on both sides.

Another common variant is to use a central, application-wide object, known as the broker, that relays messages produced by the publishers to the relevant subscribers. The broker, in this case, acts as a well-known message handler to send and subscribe to message topics. This enables us, instead of coupling different application parts together, to only reference the broker itself and also the topic that our components are interested in. Even though topics might not be an absolute requirement in the first variant of this pattern, this variant plays an essential role in scalability since there will commonly exist way less brokers (if not just one) than publishers and subscribers.

By following a subscription scheme, the code of the publisher is completely decoupled from the subscribers, meaning that the publisher does not have to know the objects depend on them. As a result, we do not need to hard code to the publisher each separate action that should be executed on the different parts of our application. Instead, the components of an application, and possibly third-party extensions, subscribe to be notified only about topics/events that they need to know. In such distributed architecture, adding a new feature to an existing application requires minimal to no changes to the application components it depends on.

How it differs from the Observer Pattern

The most basic difference is that, by definition, the Pub/Sub Pattern is a one-way-Messaging Pattern that can also pass a message, unlike the Observer Pattern that just describes how to notify the observers about a specific state change on the subject.

Moreover, unlike the Observer Pattern, the Pub/Sub Pattern with a broker results in more loosely coupled code for the different parts of an implementation. This is because the observers need to know their subject that is emitting the events; however, on the other hand, the publishers and their subscribers only need to know the broker that is used.

主站蜘蛛池模板: 东莞市| 合水县| 三河市| 横峰县| 盐城市| 长武县| 卢氏县| 徐汇区| 那曲县| 阜新市| 临沂市| 昭通市| 弥勒县| 临漳县| 得荣县| 张掖市| 历史| 利辛县| 龙门县| 凭祥市| 湘潭市| 定安县| 荣成市| 永安市| 鄂尔多斯市| 越西县| 夏邑县| 汝阳县| 石门县| 吉隆县| 襄汾县| 新乡市| 巍山| 石景山区| 林西县| 田东县| 蛟河市| 新宾| 永吉县| 霞浦县| 西丰县|