[[https://www.youtube.com/watch?v=frGy-nGoGUY|Proxy pattern diagram]], [[helloworld:design_pattern:publish_subscribe|Archive]]
Dans l'exemple ci-dessous, la classe ''Publisher'' aurait pu être intégrée dans ''PubSubServer''.
package com.llgc.publisher;
public class Message {
private String payLoad;
private String topic;
public String getPayLoad() {
return payLoad;
}
public String getTopic() {
return topic;
}
public Message(String topic, String payLoad) {
super();
this.topic = topic;
this.payLoad = payLoad;
}
}
package com.llgc.publisher;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
public class Subscriber {
private Queue topics;
private Queue myMessages;
public Subscriber() {
topics = new LinkedList();
myMessages = new LinkedList();
}
public void listen(String newTopic) {
topics.add(newTopic);
}
public void print() {
while (!myMessages.isEmpty()) {
Message newMessage = myMessages.poll();
System.out.println("Topic : " + newMessage.getTopic() + System.lineSeparator() + newMessage.getPayLoad());
}
}
public Collection getTopics() {
return Collections.unmodifiableCollection(topics);
}
public void addMessage(Message message) {
myMessages.add(message);
}
}
package com.llgc.publisher;
import java.util.LinkedList;
import java.util.Queue;
public class PubSubServer {
private Queue buffer;
private Queue subscribers;
public PubSubServer() {
buffer = new LinkedList();
subscribers = new LinkedList();
}
public void forward() {
while (!buffer.isEmpty()) {
Message tempMessage = buffer.poll();
for (Subscriber subscriber : subscribers) {
for (String topic : subscriber.getTopics()) {
if (tempMessage.getTopic().equals(topic)) {
subscriber.addMessage(tempMessage);
}
}
}
}
}
public void addSubscriber(Subscriber sub) {
subscribers.add(sub);
}
public void addBuffer(Message message) {
buffer.add(message);
}
}
package com.llgc.publisher;
public class Publisher {
public void send(Message newMessage, PubSubServer myServer) {
myServer.addBuffer(newMessage);
}
}
package com.llgc.publisher;
public class Main {
public static void main(String[] args) {
Publisher publisher = new Publisher();
Subscriber animalLover = new Subscriber();
Subscriber oldCatLady = new Subscriber();
PubSubServer server = new PubSubServer();
Message dogMessage = new Message("Dogs", "Dogs are man's best friend");
Message catMessage = new Message("Cats", "Cats can take care of themselves");
// le publieur envoie un message qui sera conservé sur le serveur.
publisher.send(dogMessage, server);
publisher.send(catMessage, server);
// On indique quels évènements chaque futur subscriber recevra.
animalLover.listen("Dogs");
animalLover.listen("Cats");
oldCatLady.listen("Cats");
// On enregistre les subscribers.
server.addSubscriber(animalLover);
server.addSubscriber(oldCatLady);
// Le serveur fait suivre les messages en attente aux subscribers.
server.forward();
// On demande au subscriber de traiter les informations qu'il a reçu.
System.out.println("animalLover has subcribed to the following messages :");
animalLover.print();
System.out.println("oldCatLady has subcribed to the following messages :");
oldCatLady.print();
}
}