Thank you, for the nice link. I have also not got the idea very
clearly, and I think it is due to the complication of the java language:
Athens sends an army against Darius the Persian.
Athens wishes to perform as a victory handler.
City Athens = new City (Greece);
1. Athens instructs her general, Miltiades: "Let us know immediately if
you are successful."
She registers her intention of handling victory events with the
source of such events: Miltiades.
General Miltiades = new General(Athens);
Miltiades.addVictoryHandler(new
MarathonHandler(Athens));
My first reaction was: Where does MarathonHandler come from? Why must
it carry Athens as the parameter?
I think this is the way to appoint Miltiades as the General who can
inform Athens as a victory handler. The new MarathonHandler part must
know that it has to inform Athens as the victory handler.
2. The Athenian army meets Darius' army at Marathon and defeats the
Persians decisively.
An event (small "e") occurs. Miltiades must inform all
registered victory handlers (just one here).
class General
{ ...
public void fightBattle(Enemy enemy, Place place,
Date date)
{ ...
winBattle(this, enemy, place, date);
}
public void winBattle(Enemy enemy, Place place,
Date date)
{ ...
notifyVictoryHandlers(new
VictoryEvent(this, enemy, place, date));
}
public void addVictoryHandler(VictoryHandler h)
{ ...
}
}
I don't seen any event (small "e") in the code. So, how does the event
play in this role? I guessed when the winBattle method is executed, the
event occured and a method of notifyVictoryHandlers are called, which in
turn create a new object of VictoryEvent.
The next question is: what is the use of addVictoryHandler method?
Oh, I got it, it is a method that was called by Miltiades when he was
appointed as Athens' General. Now, what is the VictoryHandler h
parameter doing in the method and what is the relationship with
MarathonHandler(Athens) Object that was used by Miltiades?
3. Miltiades sends a runner, Pheidippides, to Athens to inform the city
of the event.
The Victory Event object (capital "E") is sent from the event
source to each victory handler.
class MarathonHandler implements VictoryHandler
{ public MarathonHandler(City city)
{ this.city = city;
}
public void victoryPerformed(VictoryEvent evt)
{ city.rejoice();
}
private City city = null;
}
Apparently, MarathonHandler is a class that implements VictoryHandler
interface, but where is Pheidippides registered as a runner? I guessed
Pheidippides is the MarathonHandler, so we could declare instead: class
Pheidippides implements VictoryHandler {...}
Now, what is the use of VictoryHandler interface? What if we just
declare class MarathonHandler without implementing any interface? Is
implementing VictoryHandler only for the sake of ensuring the
implementation victoryPerformed method?
I hope somebody can enlighten me.
Thanks in advance,
Patrick
On Mon, 2009-02-23 at 10:59 -0800, miga wrote:
>
>
> On Feb 23, 7:14 pm, Tanya Dina Ruttenberg <[email protected]>
> wrote:
> > Hi Class,
> > I haven't seen any discussion on this list about the Events homework lab.
> > I understood the basic Events lab, with the ButtonHandler, but I don't
> > understand the subsequent lab where you create your own event at all.
> And maybe this link may help you understand what it is in a funnier
> way:
>
> http://csis.pace.edu/~bergin/Java/JavaEventStory.html
> >
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---