public
class
Ball
{
public
event
EventHandler BallInPlay;
public
void
OnBallInPlay(BallEventArgs e)
{
if
(BallInPlay !=
null
)
{
BallInPlay(
this
, e);
}
}
}
public
class
BallEventArgs : EventArgs
{
public
int
Trajectory {
get
;
private
set
; }
public
int
Distance {
get
;
private
set
; }
public
BallEventArgs(
int
Trajectory,
int
Distance)
{
this
.Trajectory = Trajectory;
this
.Distance = Distance;
}
}
public
class
Fan
{
public
Fan(Ball ball)
{
ball.BallInPlay +=
new
EventHandler(ball_BallInPlay);
}
public
void
ball_BallInPlay(
object
sender, EventArgs e)
{
if
(e
is
BallEventArgs)
{
BallEventArgs ballEventArgs = e
as
BallEventArgs;
if
(ballEventArgs.Distance > 400 && ballEventArgs.Trajectory > 30)
{
Console.WriteLine(
"Fan: Home Run! I'm going for the ball!"
);
}
else
{
Console.WriteLine(
"Fan: Woo-hoo! Yeah!"
);
}
}
}
}
public
class
Pitcher
{
public
Pitcher(Ball ball)
{
ball.BallInPlay +=
new
EventHandler(ball_BallInPlay);
}
void
ball_BallInPlay(
object
sender, EventArgs e)
{
if
(e
is
BallEventArgs)
{
BallEventArgs ballEventArgs = e
as
BallEventArgs;
if
(ballEventArgs.Distance < 95 && ballEventArgs.Trajectory < 60)
{
CatchBall();
}
else
{
CorverFirstBase();
}
}
}
private
void
CatchBall()
{
Console.WriteLine(
"Pitcher: I caught the ball!"
);
}
private
void
CorverFirstBase()
{
Console.WriteLine(
"Pitcher: I corvered first base."
);
}
}
class
Program
{
static
void
Main(
string
[] args)
{
Ball ball =
new
Ball();
Pitcher pitcher =
new
Pitcher(ball);
Fan fan =
new
Fan(ball);
BallEventArgs ballEventArgs =
new
BallEventArgs(100, 40);
ball.OnBallInPlay(ballEventArgs);
}
}