
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,
- public class Weather {
- public string WeatherNow {
- get;
- set;
- }
- public string Country {
- get;
- set;
- }
- }
- public class WeatherDB {
- public static IList < Weather > Weathers = new List < Weather > () {
- new Weather {
- WeatherNow = "Hot", Country = "US"
- }, new Weather {
- WeatherNow = "Cold", Country = "UK"
- }, new Weather {
- WeatherNow = "Moderate", Country = "EG"
- }
- };
- }
and IWeatherSerivce,
- public interface IWeatherService
- {
- string GetWeather(string country);
- }
and add WeatherService to implement the IWeatherService
- public class WeatherService: IWeatherService {
- public string GetWeather(string country) {
- return WeatherDB.Weathers.FirstOrDefault(a => a.Country == country)?.WeatherNow;
- }
- }
and in program.cs call WeatherService
- IWeatherService weatherService = new WeatherService();
- 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.


Join the conversation! Your thoughts help the community grow.