diff --git a/solutions/java/src/LLDRunner.java b/solutions/java/src/LLDRunner.java index f08e330..62cb026 100644 --- a/solutions/java/src/LLDRunner.java +++ b/solutions/java/src/LLDRunner.java @@ -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; @@ -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(); diff --git a/solutions/java/src/pubsubsystem/ConcreteSubscriber.java b/solutions/java/src/pubsubsystem/PrintSubscriber.java similarity index 71% rename from solutions/java/src/pubsubsystem/ConcreteSubscriber.java rename to solutions/java/src/pubsubsystem/PrintSubscriber.java index f2c6164..84b08bc 100644 --- a/solutions/java/src/pubsubsystem/ConcreteSubscriber.java +++ b/solutions/java/src/pubsubsystem/PrintSubscriber.java @@ -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; } diff --git a/solutions/java/src/pubsubsystem/PubSubDemo.java b/solutions/java/src/pubsubsystem/PubSubDemo.java deleted file mode 100644 index 29f9da9..0000000 --- a/solutions/java/src/pubsubsystem/PubSubDemo.java +++ /dev/null @@ -1,41 +0,0 @@ -package pubsubsystem; - -public class PubSubDemo { - public static void run() { - PubSubSystem pubSubSystem = new PubSubSystem(); - - // Create topics - pubSubSystem.createTopic("Topic1"); - pubSubSystem.createTopic("Topic2"); - - // Create subscribers - Subscriber subscriber1 = new ConcreteSubscriber("Subscriber1"); - Subscriber subscriber2 = new ConcreteSubscriber("Subscriber2"); - Subscriber subscriber3 = new ConcreteSubscriber("Subscriber3"); - - // Subscribe to topics - pubSubSystem.subscribe("Topic1", subscriber1); - pubSubSystem.subscribe("Topic1", subscriber2); - pubSubSystem.subscribe("Topic2", subscriber2); - pubSubSystem.subscribe("Topic2", subscriber3); - - // Create publishers - Publisher publisher1 = new Publisher(pubSubSystem.getTopics().get("Topic1")); - Publisher publisher2 = new Publisher(pubSubSystem.getTopics().get("Topic2")); - - // Publish messages - publisher1.publish(new Message("Message1 for Topic1")); - publisher1.publish(new Message("Message2 for Topic1")); - publisher2.publish(new Message("Message1 for Topic2")); - - // Unsubscribe from a topic - pubSubSystem.unsubscribe("Topic1", subscriber2); - - // Publish more messages - publisher1.publish(new Message("Message3 for Topic1")); - publisher2.publish(new Message("Message2 for Topic2")); - - // Shutdown the system - pubSubSystem.shutdown(); - } -} diff --git a/solutions/java/src/pubsubsystem/PubSubSystem.java b/solutions/java/src/pubsubsystem/PubSubSystem.java deleted file mode 100644 index a814b61..0000000 --- a/solutions/java/src/pubsubsystem/PubSubSystem.java +++ /dev/null @@ -1,44 +0,0 @@ -package pubsubsystem; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -public class PubSubSystem { - private final Map topics = new ConcurrentHashMap<>(); - private final ExecutorService executorService = Executors.newFixedThreadPool(10); - - public void createTopic(String topicName) { - topics.putIfAbsent(topicName, new Topic(topicName)); - } - - public void subscribe(String topicName, Subscriber subscriber) { - Topic topic = topics.get(topicName); - if (topic != null) { - topic.addSubscriber(subscriber); - } - } - - public void unsubscribe(String topicName, Subscriber subscriber) { - Topic topic = topics.get(topicName); - if (topic != null) { - topic.removeSubscriber(subscriber); - } - } - - public void publish(String topicName, Message message) { - Topic topic = topics.get(topicName); - if (topic != null) { - executorService.submit(() -> topic.publish(message)); - } - } - - public void shutdown() { - executorService.shutdown(); - } - - public Map getTopics() { - return topics; - } -} diff --git a/solutions/java/src/pubsubsystem/PubSubSystemDemo.java b/solutions/java/src/pubsubsystem/PubSubSystemDemo.java new file mode 100644 index 0000000..39788c6 --- /dev/null +++ b/solutions/java/src/pubsubsystem/PubSubSystemDemo.java @@ -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")); + } +} diff --git a/solutions/java/src/pubsubsystem/Publisher.java b/solutions/java/src/pubsubsystem/Publisher.java index c62a339..2b72930 100644 --- a/solutions/java/src/pubsubsystem/Publisher.java +++ b/solutions/java/src/pubsubsystem/Publisher.java @@ -1,13 +1,24 @@ package pubsubsystem; +import java.util.HashSet; +import java.util.Set; + public class Publisher { - private final Topic topic; + private final Set 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); } }