Skip to content

Commit bb5f9a6

Browse files
committed
fix: fix documentation on publisher-subscriber design pattern (iluwatar#2898)
1 parent 92bc114 commit bb5f9a6

File tree

12 files changed

+26
-46
lines changed

12 files changed

+26
-46
lines changed

publish-subscribe/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Publisher-Subscriber Pattern in Java: Decoupling the solution with asynchronous communication"
2+
title: "Publish-Subscribe Pattern in Java: Decoupling the solution with asynchronous communication"
33
shortTitle: Proxy
44
description: "Explore the Proxy design pattern in Java with detailed examples. Learn how it provides controlled access, facilitates lazy initialization, and ensures security. Ideal for developers looking to implement advanced Java techniques."
55
category: Structural
@@ -14,14 +14,14 @@ tag:
1414
- Wrapping
1515
---
1616

17-
## Intent of the Publisher-Subscriber Design Pattern
17+
## Intent of the Publish-Subscribe Design Pattern
1818

19-
The publisher-subscriber design pattern is widely used in software architecture to transmit data between various components in a system.
19+
The Publish-Subscriber design pattern is widely used in software architecture to transmit data between various components in a system.
2020
It is a behavioral design pattern aimed at achieving loosely coupled communication between objects.
2121
The primary intent is to allow a one-to-many dependency relationship where one object (the Publisher) notifies multiple other objects (the Subscribers) about changes or events,
2222
without needing to know who or what the subscribers are.
2323

24-
## Detailed Explanation of Publisher-Subscriber Pattern with Real-World Examples
24+
## Detailed Explanation of Publish-Subscribe Pattern with Real-World Examples
2525

2626
- Messaging systems like Kafka, RabbitMQ, AWS SNS, JMS
2727
- **Kafka** : publishes messages to topics and subscribers consumes them in real time for analytics, logs or other purposes.
@@ -38,7 +38,7 @@ without needing to know who or what the subscribers are.
3838
- **Publisher** : Writes a new blog post and publish to subscribers
3939
- **Subscribers** : All the subscribers to the newsletter receive the email
4040

41-
## Programmatic Example of Publisher-Subscriber Pattern in Java
41+
## Programmatic Example of Publish-Subscribe Pattern in Java
4242

4343
First we need to identify the Event on which we need the pub-sub methods to trigger.
4444
For example:
@@ -157,7 +157,7 @@ Program output:
157157
11:46:44.311 [main] INFO com.iluwatar.publish.subscribe.subscriber.CustomerSupportSubscriber - Subscriber: supportSub2 sent the email to: [email protected]
158158
```
159159

160-
## When to Use the Publisher-Subscriber Pattern
160+
## When to Use the Publish-Subscribe Pattern
161161

162162
- Event-Driven Systems
163163
- Use Pub/Sub when your system relies on events (e.g., user registration, payment completion).
@@ -183,13 +183,13 @@ Program output:
183183
- Allow independent services to communicate without direct coupling.
184184
- Example: An order service publishes an event, and both the billing and shipping services process it.
185185

186-
## When to avoid the Publisher-Subscriber Pattern
186+
## When to avoid the Publish-Subscribe Pattern
187187

188188
- Simple applications where direct calls suffice.
189189
- Strong consistency requirements (e.g., banking transactions).
190190
- Low-latency synchronous communication needed.
191191

192-
## Benefits and Trade-offs of Publisher-Subscriber Pattern
192+
## Benefits and Trade-offs of Publish-Subscribe Pattern
193193

194194
### Benefits:
195195

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/App.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
* added to the system without significant changes to the existing components, making the system
2828
* highly adaptable to evolving requirements.</li></p>
2929
*
30-
* <p>In this example we will create two topics {@link TopicName} WEATHER and CUSTOMER_SUPPORT.
31-
* then we will register those topics in {@link Publisher}
32-
* we will create two {@link WeatherSubscriber} to WEATHER {@link Topic}
33-
* we will create two {@link CustomerSupportSubscriber} to CUSTOMER_SUPPORT {@link Topic}
34-
* then we will publish the two {@link Topic} with different content in the {@link Message}
30+
* <p>In this example we will create two {@link TopicName}s WEATHER and CUSTOMER_SUPPORT.
31+
* Then we will register those topics in the {@link Publisher}.
32+
* After that we will create two {@link WeatherSubscriber}s to WEATHER {@link Topic}.
33+
* Also, we will create two {@link CustomerSupportSubscriber}s to CUSTOMER_SUPPORT {@link Topic}.
34+
* Now we can publish the two {@link Topic}s with different content in the {@link Message}s.
3535
* And we can observe the output in the log where,
36-
* {@link WeatherSubscriber} publish the message with {@link WeatherContent}
37-
* {@link CustomerSupportSubscriber} publish the message with {@link CustomerSupportContent}
36+
* {@link WeatherSubscriber} will output the message with {@link WeatherContent}.
37+
* {@link CustomerSupportSubscriber} will output the message with {@link CustomerSupportContent}.
3838
* Each subscriber is only listening to the subscribed topic.
3939
*/
4040
public class App {

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/CustomerSupportContent.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.iluwatar.publish.subscribe.model;
22

3-
/**
4-
* This enum defines the content for {@link Topic} CUSTOMER_SUPPORT.
5-
*/
3+
/** This enum defines the content for {@link Topic} CUSTOMER_SUPPORT. */
64
public enum CustomerSupportContent {
75

86
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.iluwatar.publish.subscribe.model;
22

3-
/**
4-
* This class represents a Message that holds the published content.
5-
*/
3+
/** This class represents a Message that holds the published content. */
64
public record Message(Object content) {
75
}

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/Topic.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import java.util.Set;
55
import java.util.concurrent.CopyOnWriteArraySet;
66

7-
/**
8-
* This class represents a Topic that topic name and subscribers.
9-
*/
7+
/** This class represents a Topic that topic name and subscribers. */
108
public class Topic {
119

1210
private final TopicName name;

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/TopicName.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.iluwatar.publish.subscribe.model;
22

3-
/**
4-
* This enum defines the available topic names to be used in {@link Topic}.
5-
*/
3+
/** This enum defines the available topic names to be used in {@link Topic}. */
64
public enum TopicName {
75
WEATHER,
86
CUSTOMER_SUPPORT

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/model/WeatherContent.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.iluwatar.publish.subscribe.model;
22

3-
/**
4-
* This enum defines the content for {@link Topic} WEATHER.
5-
*/
3+
/** This enum defines the content for {@link Topic} WEATHER. */
64
public enum WeatherContent {
75
earthquake("earthquake tsunami warning"),
86
flood("flood start evacuation"),

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/Publisher.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import com.iluwatar.publish.subscribe.model.Message;
44
import com.iluwatar.publish.subscribe.model.Topic;
55

6-
/**
7-
* This class represents a Publisher.
8-
*/
6+
/** This class represents a Publisher. */
97
public interface Publisher {
108

119
/**
@@ -18,7 +16,7 @@ public interface Publisher {
1816
/**
1917
* Register a topic in the publisher.
2018
*
21-
* @param topic the topic to publish the message under
19+
* @param topic the topic to publish the message under
2220
* @param message message with content to be published
2321
*/
2422
void publish(Topic topic, Message message);

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/publisher/PublisherImpl.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

10-
/**
11-
* This class is an implementation of the Publisher.
12-
*/
10+
/** This class is an implementation of the Publisher. */
1311
public class PublisherImpl implements Publisher {
1412

1513
private static final Logger logger = LoggerFactory.getLogger(PublisherImpl.class);

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/CustomerSupportSubscriber.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9-
/**
10-
* This class subscribes to CUSTOMER_SUPPORT topic.
11-
*/
9+
/** This class subscribes to CUSTOMER_SUPPORT topic. */
1210
@Slf4j
1311
public class CustomerSupportSubscriber implements Subscriber {
1412

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/Subscriber.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import com.iluwatar.publish.subscribe.model.Message;
44

5-
/**
6-
* This class represents a Subscriber.
7-
*/
5+
/** This class represents a Subscriber. */
86
public interface Subscriber {
97

108
/**

publish-subscribe/src/main/java/com/iluwatar/publish/subscribe/subscriber/WeatherSubscriber.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

10-
/**
11-
* This class subscribes to WEATHER topic.
12-
*/
10+
/** This class subscribes to WEATHER topic. */
1311
@Slf4j
1412
public class WeatherSubscriber implements Subscriber {
1513

0 commit comments

Comments
 (0)