forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
59 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
.../src/pubsubsystem/ConcreteSubscriber.java → ...ava/src/pubsubsystem/PrintSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package pubsubsystem; | ||
|
||
public class PubSubSystemDemo { | ||
public static void run() { | ||
// Create topics | ||
Topic topic1 = new Topic("Topic1"); | ||
Topic topic2 = new Topic("Topic2"); | ||
|
||
// Create publishers | ||
Publisher publisher1 = new Publisher(); | ||
Publisher publisher2 = new Publisher(); | ||
|
||
// Create subscribers | ||
Subscriber subscriber1 = new PrintSubscriber("Subscriber1"); | ||
Subscriber subscriber2 = new PrintSubscriber("Subscriber2"); | ||
Subscriber subscriber3 = new PrintSubscriber("Subscriber3"); | ||
|
||
publisher1.registerTopic(topic1); | ||
publisher2.registerTopic(topic2); | ||
|
||
// Subscribe to topics | ||
topic1.addSubscriber(subscriber1); | ||
topic1.addSubscriber(subscriber2); | ||
topic2.addSubscriber(subscriber2); | ||
topic2.addSubscriber(subscriber3); | ||
|
||
// Publish messages | ||
publisher1.publish(topic1, new Message("Message1 for Topic1")); | ||
publisher1.publish(topic1, new Message("Message2 for Topic1")); | ||
publisher2.publish(topic2, new Message("Message1 for Topic2")); | ||
|
||
|
||
// Unsubscribe from a topic | ||
topic1.removeSubscriber(subscriber2); | ||
|
||
// Publish more messages | ||
publisher1.publish(topic1, new Message("Message3 for Topic1")); | ||
publisher2.publish(topic2, new Message("Message2 for Topic2")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
package pubsubsystem; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class Publisher { | ||
private final Topic topic; | ||
private final Set<Topic> topics; | ||
|
||
public Publisher() { | ||
this.topics = new HashSet<>(); | ||
} | ||
|
||
public Publisher(Topic topic) { | ||
this.topic = topic; | ||
public void registerTopic(Topic topic) { | ||
topics.add(topic); | ||
} | ||
|
||
public void publish(Message message) { | ||
public void publish(Topic topic, Message message) { | ||
if(!topics.contains(topic)) { | ||
System.out.println("This publisher can't publish to topic: " + topic.getName()); | ||
return; | ||
} | ||
topic.publish(message); | ||
} | ||
} |