1:/**
   2: * Observer Design Pattern -- "Gossip" example for COM1204 Summer 2003.
   3: * This is the driver/tester class.
   4: * <br>Spaces and TalkerListeners are created and the latter register to
   5: * listen to certain spaces. Then the TalkerListeners talk and what
   6: * they hear is reported as a printout.
   7: *
   8: * @author Bob Futrelle
   9: * @version 0.1, 28 June 2003
  10: *
  11: */
  12: public class Gossip {
  13:    
  14:    public static void main(String[] args) {
  15:    
  16:        // First, set up the spaces, talkers and register them
  17:        Space outdoorTable = new Space("Outdoor table");
  18:        Space insideTable = new Space("Inside table");
  19:        Space inbetweenTable = new Space("Inbetween table");
  20:        
  21:        // Jing is the first talker, at the outdoor table.
  22:        TalkerListener jing = new TalkerListener("Jing", outdoorTable);
  23:        // Jing registers to listen to two Spaces.
  24:        outdoorTable.iCanHearThere(jing);
  25:        inbetweenTable.iCanHearThere(jing);
  26:        
  27:        // Bob is the second talker, at the same table
  28:        TalkerListener bob = new TalkerListener("Bob", outdoorTable);
  29:        // Bob also registers to listen to the same two Spaces.
  30:        outdoorTable.iCanHearThere(bob); 
  31:        inbetweenTable.iCanHearThere(bob);
  32:        
  33:        TalkerListener elliott =
  34:            new TalkerListener("Elliot, from the Matrix", inbetweenTable);
  35:        // Elliott can't hear much, only himself.
  36:        inbetweenTable.iCanHearThere(elliott);  
  37:        
  38:        // Vorzdun monitors all; all herhisherherhis talk is telepathic
  39:        TalkerListener vorzdun = 
  40:            new TalkerListener("Vorzdun from Planet 9",insideTable);
  41:        // Of course, Vorzdun  is registered to listen to all Spaces.
  42:        outdoorTable.iCanHearThere(vorzdun); 
  43:        inbetweenTable.iCanHearThere(vorzdun);      
  44:        insideTable.iCanHearThere(vorzdun); 
  45:        
  46:        // Let the gossipping begin
  47:        bob.says("I heard that there's a Matrix agent nearby.");
  48:        jing.says("So what, we're all so Neo.");
  49:        bob.says("Yeah, I guess you're right.");
  50:        elliott.says("Which one of these guys is which?");
  51:        bob.says("Did you hear that? I think we have an agent.");
  52:        jing.says("Let's go up and away.");
  53:        vorzdun.transmits("Leader: It is clear that these creatures are still quite primitive.");
  54:    } // main()
  55:    
  56: } // class Gossip
  57: 
  58: // Output from the run above:
  59: /*
  60: Jing at the Outdoor table just heard: 
  61:"I heard that there's a Matrix agent nearby."
  62:from the place, the Outdoor table
  63:
  64:Vorzdun from Planet 9 at the Inside table just heard: 
  65:"I heard that there's a Matrix agent nearby."
  66:from the place, the Outdoor table
  67:
  68:Bob at the Outdoor table just heard: 
  69:"So what, we're all so Neo."
  70:from the place, the Outdoor table
  71:
  72:Vorzdun from Planet 9 at the Inside table just heard: 
  73:"So what, we're all so Neo."
  74:from the place, the Outdoor table
  75:
  76:Jing at the Outdoor table just heard: 
  77:"Yeah, I guess you're right."
  78:from the place, the Outdoor table
  79:
  80:Vorzdun from Planet 9 at the Inside table just heard: 
  81:"Yeah, I guess you're right."
  82:from the place, the Outdoor table
  83:
  84:Jing at the Outdoor table just heard: 
  85:"Which one of these guys is which?"
  86:from the place, the Inbetween table
  87:
  88:Bob at the Outdoor table just heard: 
  89:"Which one of these guys is which?"
  90:from the place, the Inbetween table
  91:
  92:Vorzdun from Planet 9 at the Inside table just heard: 
  93:"Which one of these guys is which?"
  94:from the place, the Inbetween table
  95:
  96:Jing at the Outdoor table just heard: 
  97:"Did you hear that? I think we have an agent."
  98:from the place, the Outdoor table
  99:
 100:Vorzdun from Planet 9 at the Inside table just heard: 
 101:"Did you hear that? I think we have an agent."
 102:from the place, the Outdoor table
 103:
 104:Bob at the Outdoor table just heard: 
 105:"Let's go up and away."
 106:from the place, the Outdoor table
 107:
 108:Vorzdun from Planet 9 at the Inside table just heard: 
 109:"Let's go up and away."
 110:from the place, the Outdoor table
 111:
 112:Telepathic message:
 113:"Leader: It is clear that these creatures are still quite primitive."
 114:*/