mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-14 22:21:20 +00:00
Corrected ItemWrapperContext to implement strategy pattern correctly.
This commit is contained in:
parent
f6db7aa935
commit
44cf4d4eb1
@ -7,49 +7,19 @@ namespace csharp.StrategyPatternExample
|
||||
/// <summary>
|
||||
/// This class is responsible for providing a category strategy to a given item.
|
||||
/// </summary>
|
||||
internal class CategoryStrategiesFactory
|
||||
static internal class CategoryStrategiesFactory
|
||||
{
|
||||
#region Variables
|
||||
|
||||
static private CategoryStrategiesFactory _strategiesFactory = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
private CategoryStrategiesFactory() { }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public static CategoryStrategiesFactory GetInstance()
|
||||
static public ICategoryStrategy GetCategoryStrategies(Item item)
|
||||
{
|
||||
if (_strategiesFactory == null)
|
||||
switch (item.Name)
|
||||
{
|
||||
_strategiesFactory = new CategoryStrategiesFactory();
|
||||
}
|
||||
case Global.NAME_ITEM_AGED_BRIE:
|
||||
return new OlderIsBetterStrategy();
|
||||
|
||||
return _strategiesFactory;
|
||||
}
|
||||
|
||||
public List<ICategoryStrategy> GetCategoryStrategies(Item item)
|
||||
{
|
||||
List<ICategoryStrategy> listCategoryStrategies = new List<ICategoryStrategy>();
|
||||
|
||||
if (item.Name == Global.NAME_ITEM_AGED_BRIE)
|
||||
{
|
||||
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||
{
|
||||
new OlderIsBetterStrategy()
|
||||
};
|
||||
|
||||
}
|
||||
else if (item.Name == Global.NAME_ITEM_BACKSTAGE_PASSES)
|
||||
{
|
||||
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||
{
|
||||
new CloseExpiredImproveQualityStrategy(new List<CloseExpiredImproveQualityStrategy.NextExpiredCondition>() {
|
||||
case Global.NAME_ITEM_BACKSTAGE_PASSES:
|
||||
return new CloseExpiredImproveQualityStrategy(new List<CloseExpiredImproveQualityStrategy.NextExpiredCondition>() {
|
||||
new CloseExpiredImproveQualityStrategy.NextExpiredCondition()
|
||||
{
|
||||
SellInLimit = 5,
|
||||
@ -60,31 +30,17 @@ namespace csharp.StrategyPatternExample
|
||||
SellInLimit = 10,
|
||||
Increment = 2
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
else if (item.Name == Global.NAME_ITEM_SULFURAS)
|
||||
{
|
||||
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||
{
|
||||
};
|
||||
}
|
||||
else if (item.Name == Global.NAME_ITEM_CONJURED)
|
||||
{
|
||||
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||
{
|
||||
new TwiceFastDegradeQualityStrategy()
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||
{
|
||||
new NormalDegradeStrategy()
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return listCategoryStrategies;
|
||||
case Global.NAME_ITEM_SULFURAS:
|
||||
return new DoNothingStrategy();
|
||||
|
||||
case Global.NAME_ITEM_CONJURED:
|
||||
return new TwiceFastDegradeQualityStrategy();
|
||||
|
||||
default:
|
||||
return new NormalDegradeStrategy();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -11,7 +11,7 @@ namespace csharp.StrategyPatternExample
|
||||
#region Variables
|
||||
|
||||
private Item _item;
|
||||
private List<ICategoryStrategy> listCategoryStrategies;
|
||||
private ICategoryStrategy strategy;
|
||||
|
||||
#endregion
|
||||
|
||||
@ -42,8 +42,7 @@ namespace csharp.StrategyPatternExample
|
||||
public ItemWrapperContext(Item item)
|
||||
{
|
||||
this._item = item;
|
||||
|
||||
listCategoryStrategies = CategoryStrategiesFactory.GetInstance().GetCategoryStrategies(item);
|
||||
this.strategy = CategoryStrategiesFactory.GetCategoryStrategies(item);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -52,10 +51,7 @@ namespace csharp.StrategyPatternExample
|
||||
|
||||
public void UpdateQuality()
|
||||
{
|
||||
foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies)
|
||||
{
|
||||
categoryStrategyItem.Update(this._item);
|
||||
}
|
||||
strategy.Update(this._item);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Loading…
Reference in New Issue
Block a user