Skip to content

Commit

Permalink
refactor pub sub system
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishps1 committed Jun 1, 2024
1 parent 9239dec commit 012e69a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 93 deletions.
4 changes: 2 additions & 2 deletions solutions/java/src/LLDRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import onlineshopping.OnlineShoppingServiceDemo;
import onlinestockbrokeragesystem.StockBrokerageSystemDemo;
import parkinglot.ParkingLotDemo;
import pubsubsystem.PubSubDemo;
import pubsubsystem.PubSubSystemDemo;
import restaurantmanagementsystem.RestaurantManagementDemo;
import ridesharingservice.RideSharingServiceDemo;
import snakeandladdergame.SnakeAndLadderDemo;
Expand Down Expand Up @@ -58,7 +58,7 @@ public static void main(String[] args) {
// OnlineShoppingServiceDemo.run();
// StockBrokerageSystemDemo.run();
// ParkingLotDemo.run();
// PubSubDemo.run();
// PubSubSystemDemo.run();
// RestaurantManagementDemo.run();
// RideSharingServiceDemo.run();
// SnakeAndLadderDemo.run();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pubsubsystem;

public class ConcreteSubscriber implements Subscriber {
public class PrintSubscriber implements Subscriber {
private final String name;

public ConcreteSubscriber(String name) {
public PrintSubscriber(String name) {
this.name = name;
}

Expand Down
41 changes: 0 additions & 41 deletions solutions/java/src/pubsubsystem/PubSubDemo.java

This file was deleted.

44 changes: 0 additions & 44 deletions solutions/java/src/pubsubsystem/PubSubSystem.java

This file was deleted.

40 changes: 40 additions & 0 deletions solutions/java/src/pubsubsystem/PubSubSystemDemo.java
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"));
}
}
19 changes: 15 additions & 4 deletions solutions/java/src/pubsubsystem/Publisher.java
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);
}
}

0 comments on commit 012e69a

Please sign in to comment.