- 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.
- .NET之美:.NET關(guān)鍵技術(shù)深入解析
- Java面向?qū)ο筌浖_(kāi)發(fā)
- PostgreSQL Cookbook
- Offer來(lái)了:Java面試核心知識(shí)點(diǎn)精講(原理篇)
- Learning Elixir
- Learning Neo4j 3.x(Second Edition)
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Python漫游數(shù)學(xué)王國(guó):高等數(shù)學(xué)、線性代數(shù)、數(shù)理統(tǒng)計(jì)及運(yùn)籌學(xué)
- Android Native Development Kit Cookbook
- Python時(shí)間序列預(yù)測(cè)
- Mastering Apache Maven 3
- Mastering openFrameworks:Creative Coding Demystified
- Swift 4 Protocol-Oriented Programming(Third Edition)
- RESTful Web Clients:基于超媒體的可復(fù)用客戶端
- Python硬件編程實(shí)戰(zhàn)