Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // My first approach
            var subject          = new Observer.FirstTry.Subject();
            var databaseObserver = new Observer.FirstTry.Database();
            var logObserver      = new Observer.FirstTry.Log();

            subject.Register(databaseObserver);
            subject.Register(logObserver);

            subject.setAddress("*****@*****.**");
            subject.Unregister(logObserver);
            subject.setTitle("Welcome to the observer pattern");

            // GOF approach
            var weather             = new Observer.GOF.WeatherSubject();
            var windObserver        = new Observer.GOF.WindSpeedObserver(weather);
            var temperatureObserver = new Observer.GOF.TemperatureObserver(weather);

            weather.Temperature = 33.4;
            weather.WindSpeed   = 12.3;

            //dotnet events and delegates approach
            var weather2             = new WithEventsAndDelegates.WeatherSubject();
            var windObserver2        = new WithEventsAndDelegates.WindSpeedObserver(weather2);
            var temperatureObserver2 = new WithEventsAndDelegates.TemperatureObserver(weather2);

            weather2.Temperature = 55.3;
            weather2.WindSpeed   = 9.2;
        }
Exemplo n.º 2
0
 public WindSpeedObserver(WeatherSubject subject)
 {
     subject.WindSpeedChanged += WindSpeedChangeHandler;
 }
Exemplo n.º 3
0
 public TemperatureObserver(WeatherSubject subject)
 {
     subject.TemperatureChanged += TemperatureChangeHandler;
 }