Created
February 8, 2018 16:57
-
-
Save anonymous/073d64af30b70da2fdaf74e654cd64ed to your computer and use it in GitHub Desktop.
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
interface Publisher { | |
void publish(Message m); | |
} |
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
class PublisherImpl implements Publisher { | |
private Map<Topic, Subscriber[]> subscribers; | |
public Publisher(Map<Topic, Subscriber[]> subscribers) { | |
this.subscribers = subscribers; | |
} | |
public void publish(Message m) { | |
topic = new Topic(m.getType()); | |
subscribers.getOrDefault(topic, {}).forEach(subscriber -> subscriber.handle(m)); | |
} | |
} |
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
// use case | |
publisher = new PublisherImpl(someSubscribers); | |
publisher.publish(new SomethingCoolHappened()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment