Advertisement
Dennisaa

Observer - 1

Sep 27th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PlayBall {
  4.     public class BallHitEventArgs : EventArgs {
  5.         public int Speed { get; private set; }
  6.         public int Direction { get; private set; }
  7.  
  8.         public BallHitEventArgs(int speed, int direction) {
  9.             Speed = speed;
  10.             Direction = direction;
  11.         }
  12.     }
  13. }
  14. //
  15. using System;
  16.  
  17. namespace PlayBall {
  18.     internal class BallGame {
  19.  
  20.         public event EventHandler<BallHitEventArgs> ballHit;
  21.         public event EventHandler gameOver;
  22.  
  23.         internal void StartGame(int speed, int direction) {
  24.             OnBallHit(speed, direction);
  25.             OnGameOver();
  26.         }
  27.  
  28.         private void OnGameOver() {
  29.             Console.WriteLine("[NOTIFIER] : The game is over");
  30.             if (gameOver == null) return;
  31.             gameOver(this, EventArgs.Empty);
  32.         }
  33.  
  34.         public void OnBallHit(int speed, int direction) {
  35.             var message = "[NOTIFIER] : the ball has been hit [{0}][{1}]";
  36.             Console.WriteLine(message, speed, direction);
  37.             if (ballHit == null) return;
  38.             ballHit(this, new BallHitEventArgs(speed, direction));
  39.         }
  40.     }
  41. }
  42. //
  43. using System;
  44.  
  45. namespace PlayBall {
  46.     internal class RadioListener {
  47.         public RadioListener() {
  48.         }
  49.  
  50.         internal void TunesIntoGame() {
  51.             var ballGame = new BallGame();
  52.  
  53.             //start Observing...
  54.             ballGame.ballHit += BallGame_ballHit;
  55.             ballGame.gameOver += BallGame_gameOver;
  56.  
  57.             // Play the Game...
  58.             ballGame.StartGame(20, 359);
  59.         }
  60.         private void BallGame_ballHit(object sender, BallHitEventArgs e) {
  61.             var message = "[OBSERVER]: I have been told the ball has been hit [{0}][{1}]";
  62.             Console.WriteLine(message, e.Speed, e.Direction);
  63.         }
  64.         private void BallGame_gameOver(object sender, EventArgs e) {
  65.             Console.WriteLine("[OBSERVER]: I have been told the game is over");
  66.         }
  67.     }
  68. }
  69. //
  70. using System;
  71.  
  72. namespace PlayBall {
  73.     public class Program {
  74.  
  75.         public static void Main() {
  76.             var p = new Program();
  77.             p.RunTheTest();
  78.         }
  79.         private void RunTheTest() {
  80.  
  81.             var radioListener = new RadioListener();
  82.             radioListener.TunesIntoGame();
  83.        
  84.             Console.WriteLine("That's all for now - press return twice");
  85.             Console.Read();
  86.         }
  87.     }
  88. }
  89. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement