Skip to content

Commit 8fcbb85

Browse files
committed
fix: check styles on publisher-subscriber design pattern (iluwatar#2898)
1 parent c535bb7 commit 8fcbb85

File tree

11 files changed

+107
-1
lines changed

11 files changed

+107
-1
lines changed

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

+32
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,39 @@
1111
import com.iluwatar.publish.subscribe.subscriber.Subscriber;
1212
import com.iluwatar.publish.subscribe.subscriber.WeatherSubscriber;
1313

14+
/**
15+
* <p>The Publish and Subscribe pattern is a messaging paradigm used in software architecture with
16+
* several key points: <li>Decoupling of publishers and subscribers: Publishers and subscribers
17+
* operate independently, and there's no direct link between them. This enhances the scalability and
18+
* * modularity of applications.</li><li>Event-driven communication: The pattern facilitates
19+
* event-driven architectures by allowing publishers to broadcast events without concerning
20+
* themselves with who receives the events.</li><li>Dynamic subscription: Subscribers can
21+
* dynamically choose to listen for specific events or messages they are interested in, often by
22+
* subscribing to a particular topic or channel.</li><li>Asynchronous processing: The pattern
23+
* inherently supports asynchronous message processing, enabling efficient handling of events and
24+
* improving application responsiveness.</li><li>Scalability: By decoupling senders and receivers,
25+
* the pattern can support a large number of publishers and subscribers, making it suitable for
26+
* scalable systems.</li><li>Flexibility and adaptability: New subscribers or publishers can be
27+
* added to the system without significant changes to the existing components, making the system
28+
* highly adaptable to evolving requirements.</li></p>
29+
*
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}
35+
* 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}
38+
* Each subscriber is only listening to the subscribed topic.
39+
*/
1440
public class App {
41+
42+
/**
43+
* Program entry point.
44+
*
45+
* @param args command line args
46+
*/
1547
public static void main(String[] args) {
1648

1749
final String weatherSub1Name = "weatherSub1";

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

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

3+
/**
4+
* This enum defines the content for {@link Topic} CUSTOMER_SUPPORT
5+
*/
36
public enum CustomerSupportContent {
47

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

3+
/**
4+
* This class represents a Message that holds the published content
5+
*/
36
public record Message(Object content) {
47
}

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

+28
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,55 @@
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+
*/
710
public class Topic {
811

912
private final TopicName name;
1013
private final Set<Subscriber> subscribers = new CopyOnWriteArraySet<>();
1114

15+
/**
16+
* Creates a new instance of the Topic class.
17+
*
18+
* @param name The name of the topic.
19+
*/
1220
public Topic(TopicName name) {
1321
this.name = name;
1422
}
1523

24+
/**
25+
* Get the name of the topic
26+
*
27+
* @return topic name
28+
*/
1629
public TopicName getName() {
1730
return name;
1831
}
1932

33+
/**
34+
* Add a subscriber to the list of subscribers
35+
*
36+
* @param subscriber subscriber to add
37+
*/
2038
public void addSubscriber(Subscriber subscriber) {
2139
subscribers.add(subscriber);
2240
}
2341

42+
/**
43+
* Remove a subscriber to the list of subscribers
44+
*
45+
* @param subscriber subscriber to remove
46+
*/
2447
public void removeSubscriber(Subscriber subscriber) {
2548
subscribers.remove(subscriber);
2649
}
2750

51+
/**
52+
* Publish a message to subscribers
53+
*
54+
* @param message message with content to publish
55+
*/
2856
public void publish(Message message) {
2957
for (Subscriber subscriber : subscribers) {
3058
subscriber.onMessage(message);

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

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

3+
/**
4+
* This enum defines the available topic names to be used in {@link Topic}
5+
*/
36
public enum TopicName {
47
WEATHER,
58
CUSTOMER_SUPPORT

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

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

3+
/**
4+
* This enum defines the content for {@link Topic} WEATHER
5+
*/
36
public enum WeatherContent {
47
earthquake("earthquake tsunami warning"),
58
flood("flood start evacuation"),

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@
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+
*/
69
public interface Publisher {
710

11+
/**
12+
* Register a topic in the publisher
13+
*
14+
* @param topic the topic to be registered
15+
*/
816
void registerTopic(Topic topic);
917

18+
/**
19+
* Register a topic in the publisher
20+
*
21+
* @param topic the topic to publish the message under
22+
* @param message message with content to be published
23+
*/
1024
void publish(Topic topic, Message message);
11-
}
25+
}

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

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

10+
/**
11+
* This class is an implementation of the Publisher
12+
*/
1013
public class PublisherImpl implements Publisher {
1114

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

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

+5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
import com.iluwatar.publish.subscribe.model.CustomerSupportContent;
44
import com.iluwatar.publish.subscribe.model.Message;
5+
import lombok.extern.slf4j.Slf4j;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

9+
/**
10+
* This class subscribes to CUSTOMER_SUPPORT topic
11+
*/
12+
@Slf4j
813
public class CustomerSupportSubscriber implements Subscriber {
914

1015
private static final Logger logger = LoggerFactory.getLogger(CustomerSupportSubscriber.class);

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

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

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

5+
/**
6+
* This class represents a Subscriber
7+
*/
58
public interface Subscriber {
9+
10+
/**
11+
* On message method will trigger when the subscribed event is published
12+
*
13+
* @param message the message contains the content of the published event
14+
*/
615
void onMessage(Message message);
716
}

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

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

10+
/**
11+
* This class subscribes to WEATHER topic
12+
*/
1013
@Slf4j
1114
public class WeatherSubscriber implements Subscriber {
1215

0 commit comments

Comments
 (0)