Decorator Design Pattern In WeatherService Using C#

Decorator Design Pattern In WeatherService Using C#
 
For the decorator design pattern in the weather system, I will simplify content in the console app instead of .Net.
 
The first task is we want to add a page that takes the country's name and gives the current weather in this country so we will make a list that contains all-weather countries as DB like this, 
  1. public class Weather {  
  2.     public string WeatherNow {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Country {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  
  11. public class WeatherDB {  
  12.     public static IList < Weather > Weathers = new List < Weather > () {  
  13.         new Weather {  
  14.             WeatherNow = "Hot", Country = "US"  
  15.         }, new Weather {  
  16.             WeatherNow = "Cold", Country = "UK"  
  17.         }, new Weather {  
  18.             WeatherNow = "Moderate", Country = "EG"  
  19.         }  
  20.     };  
  21. }   
and IWeatherSerivce,
  1. public interface IWeatherService  
  2. {  
  3.    string GetWeather(string country);  
  4. }  
and add WeatherService to implement the IWeatherService
  1. public class WeatherService: IWeatherService {  
  2.     public string GetWeather(string country) {  
  3.         return WeatherDB.Weathers.FirstOrDefault(a => a.Country == country)?.WeatherNow;  
  4.     }  
  5. }   
and in program.cs call WeatherService
  1. IWeatherService weatherService = new WeatherService();  
  2. Console.WriteLine(weatherService.GetWeather("US"));  
Second Task: is to log data. When client ask for weather we can do logging in the same implementation in GetWeather in WeatherService but we will violate single responsibility principle and open closed principle so we will add decorator class that implements IWeatherSerivce and composes IWeatherSerivce in the constructor like this.
  1. public class LoggingService: IWeatherService {  
  2.     private readonly IWeatherService weatherService;  
  3.     public LoggingService(IWeatherService weatherService) {  
  4.         this.weatherService = weatherService;  
  5.     }  
  6.     public string GetWeather(string country) {  
  7.         var weather = weatherService.GetWeather(country);  
  8.         Console.WriteLine($ "Logging Info: Weather Now in {country} is {weather}");  
  9.         return weather;  
  10.     }  
  11. }   
and so in program.cs we will call LoggingService
  1. #region WeatherService  
  2. IWeatherService weatherService = new WeatherService();  
  3. Console.WriteLine(weatherService.GetWeather("US"));  
  4. #endregion  
  5. Console.WriteLine("******************************");  
  6. #region LoggingService  
  7. IWeatherService weatherLogging = new LoggingService(weatherService);  
  8. Console.WriteLine(weatherLogging.GetWeather("UK"));  
  9. #endregion  
The third task is we want to cache result from client so we will and another decorator class that implements IWeatherSerivce like Logging,
  1. public class CachingWeatherService: IWeatherService {  
  2.     private readonly IWeatherService weatherService;  
  3.     public CachingWeatherService(IWeatherService weatherService) {  
  4.         this.weatherService = weatherService;  
  5.     }  
  6.     public string GetWeather(string country) {  
  7.         var weather = weatherService.GetWeather(country);  
  8.         Console.WriteLine($ "Caching Weather in Memory Info: Weather Now in {country} is {weather}");  
  9.         return weather;  
  10.     }  
  11. }   
and call in program.cs,
  1. #region WeatherService    
  2. IWeatherService weatherService = new WeatherService();    
  3. Console.WriteLine(weatherService.GetWeather("US"));    
  4. #endregion    
  5. Console.WriteLine("******************************");    
  6. #region LoggingService    
  7. IWeatherService weatherLogging = new LoggingService(weatherService);    
  8. Console.WriteLine(weatherLogging.GetWeather("UK"));    
  9. #endregion    
  10. Console.WriteLine("******************************");    
  11. #region CachingService    
  12. IWeatherService weatherCaching = new CachingWeatherService(weatherLogging);    
  13. Console.WriteLine(weatherCaching.GetWeather("EG"));    
  14. #endregion    
  15. Console.ReadKey();    
and so decorator design pattern works like an onion,
 Decorator Design Pattern In WeatherService Using C#


Next Recommended Reading Decorator Design Pattern in C#