// Is the car alive or dead?
bool carIsDead;
...
}
Next, update your current Accelerate() method to make use of this new member variable as
follows:
public void Accelerate(int delta)
{
// If the car is dead, send Exploded event.
if (carIsDead)
{
foreach (IEngineNotification sink in clientSinks)
sink.Exploded("Sorry, this car is dead...");
}
else
{
currSpeed += delta;
// Almost dead?
if (10 == (maxSpeed – currSpeed))
{
foreach (IEngineNotification sink in clientSinks)
sink.AboutToBlow("Careful buddy! Gonna blow!");
}
// Still OK!
if (currSpeed >= maxSpeed)
carIsDead = true;
else
Console.WriteLine("->CurrSpeed = " + currSpeed);
}
}
With our infrastructure in place, we can now implement our Main() method to receive the
events sent from the Car type as follows:
Do'stlaringiz bilan baham: |