/** * 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." */