December 14, 2010 Design Patterns
December 14, 2010 Design Patterns
public void GoOutside()
{
var weather = Weather.GetWeather();
string clothes = GetClothes(weather);
string accessories = GetAccessories(weather);
Console.WriteLine("Today I wore {0} and took {1}", clothes, accessories);
}
private string GetAccessories(string weather)
{
string accessories;
switch (weather)
{
case "sun":
accessories = "sunglasses";
break;
case "rain":
accessories = "umbrella";
break;
default:
accessories = "nothing";
break;
}
return accessories;
}
private string GetClothes(string weather)
{
string clothes;
switch (weather)
{
case "sun":
clothes = "T-Shirt";
break;
case "rain":
clothes = "Coat";
break;
default:
clothes = "Shirt";
break;
}
return clothes;
} internal class Myself
{
private IWearingStrategy _wearingStrategy = new DefaultWearingStrategy();
public void ChangeStrategy(IWearingStrategy wearingStrategy)
{
_wearingStrategy = wearingStrategy;
}
public void GoOutside()
{
var clothes = _wearingStrategy.GetClothes();
var accessories = _wearingStrategy.GetAccessories();
Console.WriteLine("Today I wore {0} and took {1}", clothes, accessories);
}
} public interface IWearingStrategy
{
string GetClothes();
string GetAccessories();
}
class SunshineWearingStrategy : IWearingStrategy
{
public string GetClothes()
{
return "T-Shirt";
}
public string GetAccessories()
{
return "sunglasses";
}
}All left is to correctly chose strategy and set it for Myself instance. Hm! Anyway someone have to take a look on weather and put right strategy (wife, who woke up in the morning). But we can do this in one different place.
Output: “Today I wore Coat and took umbrella”
| Markdown | Result |
|---|---|
| *text* | text |
| **text** | text |
| ***text*** | text |
| `code` | code |
| ~~~ more code ~~~~ |
more code |
| [Link](https://www.example.com) | Link |
| * Listitem |
|
| > Quote | Quote |
as always very nice explanation.
Thank you very much!