Last active
August 29, 2015 14:04
-
-
Save tamizhvendan/fb9f64cd49646ddf8eba to your computer and use it in GitHub Desktop.
Observer Pattern using Delegates V/s Observer Pattern using Events and Delegates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace DelegatesAndEvents | |
| { | |
| class EvilSubscriber | |
| { | |
| WeatherStation weatherStation; | |
| public void EvilMessage(int temperature) | |
| { | |
| Console.WriteLine("The temperature is " + (temperature * 100)); | |
| } | |
| public EvilSubscriber(WeatherStation weatherStation) | |
| { | |
| this.weatherStation = weatherStation; | |
| // Broadcast to all the subscriber | |
| weatherStation.Reporter(60); | |
| // Reassign the subscriber | |
| weatherStation.Reporter = EvilMessage; | |
| // Replace all the subscribers | |
| weatherStation.Reporter = null; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EvilSubscriber2 | |
| { | |
| WeatherStation2 weatherStation; | |
| public void EvilMessage(int temperature) | |
| { | |
| Console.WriteLine("The temperature is " + (temperature * 100)); | |
| } | |
| public EvilSubscriber2(WeatherStation2 weatherStation) | |
| { | |
| this.weatherStation = weatherStation; | |
| // Broadcast to all the subscriber | |
| weatherStation.Reporter(60); // Compiler Error | |
| // Reassign the subscriber | |
| weatherStation.Reporter = EvilMessage; // Compiler Error | |
| // Replace all the subscribers | |
| weatherStation.Reporter = null; // Compiler Error | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace DelegatesAndEvents | |
| { | |
| public delegate void TemperatureReporter(int tempearture); | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Observer Pattern Using Delegates | |
| WeatherStation weatherStation = new WeatherStation(); | |
| FlashNews flashNews = new FlashNews(weatherStation); | |
| NewsFeed newsFeed = new NewsFeed(weatherStation); | |
| weatherStation.Temperature = 30; | |
| weatherStation.Temperature = 15; | |
| weatherStation.Temperature = 45; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace DelegatesAndEvents | |
| { | |
| class WeatherStation | |
| { | |
| private int _temperature; | |
| public TemperatureReporter Reporter { get; set; } | |
| public int Temperature | |
| { | |
| get { return _temperature; } | |
| set | |
| { | |
| if (Reporter != null) | |
| { | |
| Reporter(value); | |
| } | |
| _tempearture = value; | |
| } | |
| } | |
| } | |
| class NewsFeed | |
| { | |
| WeatherStation weatherStation; | |
| public void AddTemperatureInfoToFeed(int temperature) | |
| { | |
| Console.WriteLine("News Feed: New temperature " + temperature + " added to feed."); | |
| } | |
| public NewsFeed(WeatherStation weatherStation) | |
| { | |
| this.weatherStation = weatherStation; | |
| weatherStation.Reporter += AddTemperatureInfoToFeed; | |
| } | |
| } | |
| class FlashNews | |
| { | |
| WeatherStation weatherStation; | |
| public void ShowTemperatureInfoInFlashNews(int temperature) | |
| { | |
| Console.WriteLine("Flash News: New temperature is " + temperature + "."); | |
| } | |
| public FlashNews(WeatherStation weatherStation) | |
| { | |
| this.weatherStation = weatherStation; | |
| weatherStation.Reporter += ShowTemperatureInfoInFlashNews; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace DelegatesAndEvents | |
| { | |
| class WeatherStation2 | |
| { | |
| private int _tempearture; | |
| public event TemperatureReporter Reporter; | |
| public int Temperature | |
| { | |
| get | |
| { | |
| return _tempearture; | |
| } | |
| set | |
| { | |
| if (Reporter != null) | |
| { | |
| Reporter(value); | |
| } | |
| _tempearture = value; | |
| } | |
| } | |
| } | |
| class NewsFeed2 | |
| { | |
| WeatherStation2 weatherStation; | |
| public void AddTemperatureInfoToFeed(int temperature) | |
| { | |
| Console.WriteLine("News Feed2: New temperature " + temperature + " added to feed."); | |
| } | |
| public NewsFeed2(WeatherStation2 weatherStation) | |
| { | |
| this.weatherStation = weatherStation; | |
| weatherStation.Reporter += AddTemperatureInfoToFeed; | |
| } | |
| } | |
| class FlashNews2 | |
| { | |
| WeatherStation2 weatherStation; | |
| public void ShowTemperatureInfoInFlashNews(int temperature) | |
| { | |
| Console.WriteLine("Flash News2: New temperature is " + temperature + "."); | |
| } | |
| public FlashNews2(WeatherStation2 weatherStation) | |
| { | |
| this.weatherStation = weatherStation; weatherStation.Reporter += ShowTemperatureInfoInFlashNews; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment