Skip to content

Instantly share code, notes, and snippets.

Created February 8, 2018 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/073d64af30b70da2fdaf74e654cd64ed to your computer and use it in GitHub Desktop.
Save anonymous/073d64af30b70da2fdaf74e654cd64ed to your computer and use it in GitHub Desktop.
interface Publisher {
void publish(Message m);
}
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));
}
}
// 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