Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace PlayBall {
- public class BallHitEventArgs : EventArgs {
- public int Speed { get; private set; }
- public int Direction { get; private set; }
- public BallHitEventArgs(int speed, int direction) {
- Speed = speed;
- Direction = direction;
- }
- }
- }
- //
- using System;
- namespace PlayBall {
- internal class BallGame {
- public event EventHandler<BallHitEventArgs> ballHit;
- public event EventHandler gameOver;
- internal void StartGame(int speed, int direction) {
- OnBallHit(speed, direction);
- OnGameOver();
- }
- private void OnGameOver() {
- Console.WriteLine("[NOTIFIER] : The game is over");
- if (gameOver == null) return;
- gameOver(this, EventArgs.Empty);
- }
- public void OnBallHit(int speed, int direction) {
- var message = "[NOTIFIER] : the ball has been hit [{0}][{1}]";
- Console.WriteLine(message, speed, direction);
- if (ballHit == null) return;
- ballHit(this, new BallHitEventArgs(speed, direction));
- }
- }
- }
- //
- using System;
- namespace PlayBall {
- internal class RadioListener {
- public RadioListener() {
- }
- internal void TunesIntoGame() {
- var ballGame = new BallGame();
- //start Observing...
- ballGame.ballHit += BallGame_ballHit;
- ballGame.gameOver += BallGame_gameOver;
- // Play the Game...
- ballGame.StartGame(20, 359);
- }
- private void BallGame_ballHit(object sender, BallHitEventArgs e) {
- var message = "[OBSERVER]: I have been told the ball has been hit [{0}][{1}]";
- Console.WriteLine(message, e.Speed, e.Direction);
- }
- private void BallGame_gameOver(object sender, EventArgs e) {
- Console.WriteLine("[OBSERVER]: I have been told the game is over");
- }
- }
- }
- //
- using System;
- namespace PlayBall {
- public class Program {
- public static void Main() {
- var p = new Program();
- p.RunTheTest();
- }
- private void RunTheTest() {
- var radioListener = new RadioListener();
- radioListener.TunesIntoGame();
- Console.WriteLine("That's all for now - press return twice");
- Console.Read();
- }
- }
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement