Java Reference
In-Depth Information
The following FootballGame class represents a live football game, and as
events happen during the game, the listeners of the game receive updated
information. Notice that the FootballGame class uses a Vector to keep track of
all listeners. More importantly, notice how listeners get into the Vector.
import java.util.Vector;
public class FootballGame
{
private String homeTeam, visitingTeam;
private int homeScore, visitingScore;
private Vector audience;
public FootballGame(String homeTeam, String visitingTeam)
{
this.homeTeam = homeTeam;
this.visitingTeam = visitingTeam;
audience = new Vector(10);
}
public void addFootballListener(FootballListener f)
{
//Add the listener to the Vector.
audience.add(f);
//Tell them who is playing.
f.setHomeTeam(homeTeam);
f.setVisitingTeam(visitingTeam);
}
public void homeTeamScored(int points)
{
//Notify the audience that the home team scored.
int size = audience.size();
for(int i = 0; i < size; i++)
{
FootballListener current =
(FootballListener) audience.elementAt(i);
current.homeTeamScored(points);
}
}
public void visitingTeamScored(int points)
{
//Notify the audience that the visiting team scored.
int size = audience.size();
for(int i = 0; i < size; i++)
{
FootballListener current =
(FootballListener) audience.elementAt(i);
current.visitingTeamScored(points);
}
}
public void quarterEnded(int quarter)
{
Search WWH ::




Custom Search