mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 06:21:29 +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>
|
/// <summary>
|
||||||
/// This class is responsible for providing a category strategy to a given item.
|
/// This class is responsible for providing a category strategy to a given item.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class CategoryStrategiesFactory
|
static internal class CategoryStrategiesFactory
|
||||||
{
|
{
|
||||||
#region Variables
|
|
||||||
|
|
||||||
static private CategoryStrategiesFactory _strategiesFactory = null;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Constructor
|
|
||||||
|
|
||||||
private CategoryStrategiesFactory() { }
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Methods
|
#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;
|
case Global.NAME_ITEM_BACKSTAGE_PASSES:
|
||||||
}
|
return new CloseExpiredImproveQualityStrategy(new List<CloseExpiredImproveQualityStrategy.NextExpiredCondition>() {
|
||||||
|
|
||||||
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>() {
|
|
||||||
new CloseExpiredImproveQualityStrategy.NextExpiredCondition()
|
new CloseExpiredImproveQualityStrategy.NextExpiredCondition()
|
||||||
{
|
{
|
||||||
SellInLimit = 5,
|
SellInLimit = 5,
|
||||||
@ -60,31 +30,17 @@ namespace csharp.StrategyPatternExample
|
|||||||
SellInLimit = 10,
|
SellInLimit = 10,
|
||||||
Increment = 2
|
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
|
#endregion
|
||||||
|
|||||||
@ -11,7 +11,7 @@ namespace csharp.StrategyPatternExample
|
|||||||
#region Variables
|
#region Variables
|
||||||
|
|
||||||
private Item _item;
|
private Item _item;
|
||||||
private List<ICategoryStrategy> listCategoryStrategies;
|
private ICategoryStrategy strategy;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -42,8 +42,7 @@ namespace csharp.StrategyPatternExample
|
|||||||
public ItemWrapperContext(Item item)
|
public ItemWrapperContext(Item item)
|
||||||
{
|
{
|
||||||
this._item = item;
|
this._item = item;
|
||||||
|
this.strategy = CategoryStrategiesFactory.GetCategoryStrategies(item);
|
||||||
listCategoryStrategies = CategoryStrategiesFactory.GetInstance().GetCategoryStrategies(item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -52,10 +51,7 @@ namespace csharp.StrategyPatternExample
|
|||||||
|
|
||||||
public void UpdateQuality()
|
public void UpdateQuality()
|
||||||
{
|
{
|
||||||
foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies)
|
strategy.Update(this._item);
|
||||||
{
|
|
||||||
categoryStrategyItem.Update(this._item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user