Skip to content

Instantly share code, notes, and snippets.

@revskill10
Created July 12, 2014 15:47
Show Gist options
  • Save revskill10/982fda26d13af5d7f4b1 to your computer and use it in GitHub Desktop.
Save revskill10/982fda26d13af5d7f4b1 to your computer and use it in GitHub Desktop.
Event style of FizzBuzz
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Clock c = new Clock();
Listener l = new Listener();
l.subscribe(c);
c.run();
}
}
class Listener
{
public void subscribe(Clock c)
{
c.OnFizz += new Clock.TickHandler(FizzHandler);
c.OnBuzz += new Clock.TickHandler(BuzzHandler);
c.OnFizzBuzz += new Clock.TickHandler(FizzBuzzHandler);
}
public void FizzHandler(Clock c, EventArgs e)
{
System.Console.WriteLine("Fizz");
}
public void BuzzHandler(Clock c, EventArgs e)
{
System.Console.WriteLine("Buzz");
}
public void FizzBuzzHandler(Clock c, EventArgs e)
{
System.Console.WriteLine("FizzBuzz");
}
}
class Clock
{
public event TickHandler OnFizz;
public event TickHandler OnBuzz;
public event TickHandler OnFizzBuzz;
public EventArgs e = null;
public delegate void TickHandler(Clock c, EventArgs e);
public void run()
{
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(1000);
if (i % 15 == 0)
{
OnFizzBuzz(this, e);
}
else if (i % 3 == 0)
{
OnFizz(this, e);
}
else if (i % 5 == 0)
{
OnBuzz(this, e);
}
else
{
System.Console.WriteLine(i);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment