On Feb 24, 8:00 am, Patrick_Stiady <[email protected]> wrote:
> 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?
It is the handler of marathon runners who run for Athens. A part of it
will run for the account of Miltiades, who was designated by Athens to
report victories.
> Why must it carry Athens as the parameter?
Because they could exist another cities who may have marathon handlers
to report victories events.
Or Miltiades could be appointed by Athens to report another sort of
events, say disasters.
So the system needs to know to whom reports this particular victory
event.
>
> 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.
It is more the other way around.
>
> 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?
It is not represented here. But you may imagine it.
> 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?
It is a way to say I want to be informed of that event, so the general
says I want to be inform of the victory.
Then when the event occurs, the source of events informs automatically
all the objects who have declared them interested in knowing about
that event.
> 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?
It is a way of having some functions necessary to handle events in a
same place. Then all classes which implements that interface get those
functions without doing anything.
> What if we just
> declare class MarathonHandler without implementing any interface? Is
> implementing VictoryHandler only for the sake of ensuring the
> implementation victoryPerformed method?
There are several methods implemented by the interface, one of them is
add, but if you reread the javadoc again you will see they are more.
Hope it is a little clearer now.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---