==> Gossip.java <== /** * Observer Design Pattern -- "Gossip" example for COM1204 Summer 2003. * This is the driver/tester class. *
Spaces and TalkerListeners are created and the latter register to * listen to certain spaces. Then the TalkerListeners talk and what * they hear is reported as a printout. * * @author Bob Futrelle * @version 0.1, 28 June 2003 * */ public class Gossip { public static void main(String[] args) { // First, set up the spaces, talkers and register them Space outdoorTable = new Space("Outdoor table"); Space insideTable = new Space("Inside table"); Space inbetweenTable = new Space("Inbetween table"); // Jing is the first talker, at the outdoor table. TalkerListener jing = new TalkerListener("Jing", outdoorTable); // Jing registers to listen to two Spaces. outdoorTable.addTalkListener(jing); inbetweenTable.addTalkListener(jing); // Bob is the second talker, at the same table TalkerListener bob = new TalkerListener("Bob", outdoorTable); // Bob also registers to listen to the same two Spaces. outdoorTable.addTalkListener(bob); inbetweenTable.addTalkListener(bob); TalkerListener elliott = new TalkerListener("Elliot, from the Matrix", inbetweenTable); // Elliott can't hear much, only himself. inbetweenTable.addTalkListener(elliott); // Vorzdun monitors all; all herhisherherhis talk is telepathic TalkerListener vorzdun = new TalkerListener("Vorzdun from Planet 9",insideTable); // Of course, Vorzdun is registered to listen to all Spaces. outdoorTable.addTalkListener(vorzdun); inbetweenTable.addTalkListener(vorzdun); insideTable.addTalkListener(vorzdun); // Let the gossipping begin bob.says("I heard that there's a Matrix agent nearby."); jing.says("So what, we're all so Neo."); bob.says("Yeah, I guess you're right."); elliott.says("Which one of these guys is which?"); bob.says("Did you hear that? I think we have an agent."); jing.says("Let's go up and away."); vorzdun.transmits("Leader: It is clear that these creatures are still quite primitive."); } // main() } // class Gossip // Output from the run above: /* Jing at the Outdoor table just heard: "I heard that there's a Matrix agent nearby." from the place, the Outdoor table Vorzdun from Planet 9 at the Inside table just heard: "I heard that there's a Matrix agent nearby." from the place, the Outdoor table Bob at the Outdoor table just heard: "So what, we're all so Neo." from the place, the Outdoor table Vorzdun from Planet 9 at the Inside table just heard: "So what, we're all so Neo." from the place, the Outdoor table Jing at the Outdoor table just heard: "Yeah, I guess you're right." from the place, the Outdoor table Vorzdun from Planet 9 at the Inside table just heard: "Yeah, I guess you're right." from the place, the Outdoor table Jing at the Outdoor table just heard: "Which one of these guys is which?" from the place, the Inbetween table Bob at the Outdoor table just heard: "Which one of these guys is which?" from the place, the Inbetween table Vorzdun from Planet 9 at the Inside table just heard: "Which one of these guys is which?" from the place, the Inbetween table Jing at the Outdoor table just heard: "Did you hear that? I think we have an agent." from the place, the Outdoor table Vorzdun from Planet 9 at the Inside table just heard: "Did you hear that? I think we have an agent." from the place, the Outdoor table Bob at the Outdoor table just heard: "Let's go up and away." from the place, the Outdoor table Vorzdun from Planet 9 at the Inside table just heard: "Let's go up and away." from the place, the Outdoor table Telepathic message: "Leader: It is clear that these creatures are still quite primitive." */ ==> Message.java <== /** * The Message classes bundles the TalkerListener speaking and what they say. * This allows the recipient to find the place at which the message was generated. *
Observer Design Pattern -- "Gossip" example for COM1204 Summer 2003. * * @author Bob Futrelle * @version 0.1, 28 June 2003 * */ public class Message { public TalkerListener talkerListener; public String whatTalkerSaid; public Message(TalkerListener talkerListener, String whatTalkerSaid){ this.talkerListener = talkerListener; this.whatTalkerSaid = whatTalkerSaid; } } // class Messagew ==> Space.java <== import java.util.*; /** * The Space class where messages are sent and which notifies registered listeners. *
Observer Design Pattern -- "Gossip" example for COM1204 Summer 2003. * * @author Bob Futrelle * @version 0.1, 28 June 2003 * */ public class Space { public String name; public ArrayList registeredListeners; public Space(String name){ this.name = name; registeredListeners = new ArrayList(); } /** Allows a TalkerListener to be placed on the registered listeners list. */ public void addTalkListener(TalkerListener listener) { registeredListeners.add(listener); } /** When a TalkerListener says something, its local space is informed. */ public void notifySpace(Message msg) { Iterator listeners = registeredListeners.iterator(); while(listeners.hasNext()){ ((TalkerListener)listeners.next()).notifyMe(msg); } } } // class Space ==> TalkerListener.java <== /** * The TalkerListener "talks" in a space and registers to "listen" to other spaces. *
Observer Design Pattern -- "Gossip" example for COM1204 Summer 2003. * * @author Bob Futrelle * @version 0.1, 28 June 2003 * */ public class TalkerListener { public String name; public Space whereIAmAt; /** The TalkerListener has a name and a location */ public TalkerListener(String name, Space whereIAmAt){ this.name = name; this.whereIAmAt = whereIAmAt; } /** When the TalkerListener speaks, the local space is "notified". */ public void says(String said) { whereIAmAt.notifySpace( new Message(this,said)); } /** When a space that the TalkerListener has registered with receives * a message, it notifies the TalkerListener using this method. */ public void notifyMe(Message msg) { // Don't report what you yourself just said. if(msg.talkerListener != this) System.out.println(name + " at the " + whereIAmAt.name + " just heard: \n" + "\"" + msg.whatTalkerSaid + "\"\nfrom the place, the " + msg.talkerListener.whereIAmAt.name + "\n"); } /** Tells us the contents of any telepathic message being sent. */ public void transmits(String telepathic) { System.out.println("Telepathic message:\n\"" + telepathic + "\""); } } // class TalkerListener