mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 06:21:29 +00:00
· Insert comments.
· DoNothingStrategy created.
This commit is contained in:
parent
e333daef74
commit
f6db7aa935
@ -1,4 +1,5 @@
|
||||
using NUnit.Framework;
|
||||
using csharp.StrategyPatternExample;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace csharp
|
||||
@ -6,19 +7,10 @@ namespace csharp
|
||||
[TestFixture]
|
||||
public class GildedRoseTest
|
||||
{
|
||||
[Test]
|
||||
public void foo()
|
||||
{
|
||||
IList<Item> Items = new List<Item> { new Item { Name = "foo", SellIn = 0, Quality = 0 } };
|
||||
GildedRose app = new GildedRose(Items);
|
||||
app.UpdateQuality();
|
||||
Assert.AreNotEqual("fixme", Items[0].Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConjuredQuality()
|
||||
{
|
||||
IList<Item> Items = new List<Item> { new Item { Name = "Conjured Mana Cake", SellIn = 1, Quality = 8 } };
|
||||
IList<Item> Items = new List<Item> { new Item { Name = Global.NAME_ITEM_CONJURED, SellIn = 1, Quality = 8 } };
|
||||
GildedRose app = new GildedRose(Items);
|
||||
|
||||
// "Conjured" items degrade in Quality twice as fast as normal items.
|
||||
@ -31,5 +23,24 @@ namespace csharp
|
||||
app.UpdateQuality();
|
||||
Assert.AreEqual(2, Items[0].Quality);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConjuredQuality_StrategyPatternExample()
|
||||
{
|
||||
IList<Item> Items = new List<Item> { new Item { Name = Global.NAME_ITEM_CONJURED, SellIn = 1, Quality = 8 } };
|
||||
GildedRoseStrategyPatternExample app = new GildedRoseStrategyPatternExample(Items);
|
||||
|
||||
// "Conjured" items degrade in Quality twice as fast as normal items.
|
||||
// So,
|
||||
// SellIn >= 0 => degrade = -2
|
||||
// SellIn < 0 => degrade = -4
|
||||
app.UpdateQuality();
|
||||
Assert.AreEqual(6, Items[0].Quality);
|
||||
|
||||
app.UpdateQuality();
|
||||
Assert.AreEqual(2, Items[0].Quality);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,10 @@ using System.Collections.Generic;
|
||||
|
||||
namespace csharp.StrategyPatternExample
|
||||
{
|
||||
class CategoryStrategiesFactory
|
||||
/// <summary>
|
||||
/// This class is responsible for providing a category strategy to a given item.
|
||||
/// </summary>
|
||||
internal class CategoryStrategiesFactory
|
||||
{
|
||||
#region Variables
|
||||
|
||||
|
||||
@ -4,15 +4,28 @@ using csharp.StrategyPatternExample;
|
||||
|
||||
namespace csharp.StrategyPatternExample
|
||||
{
|
||||
/// <summary>
|
||||
/// GildedRose class refactored to adapt the strategy pattern without making a great refactor of main method of Program.cs.
|
||||
/// </summary>
|
||||
public class GildedRoseStrategyPatternExample : IGildedRoseApp
|
||||
{
|
||||
#region Variables
|
||||
|
||||
private IList<ItemWrapperContext> Items;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public GildedRoseStrategyPatternExample(IList<Item> Items)
|
||||
{
|
||||
this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList<ItemWrapperContext>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public void UpdateQuality()
|
||||
{
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
@ -20,5 +33,7 @@ namespace csharp.StrategyPatternExample
|
||||
Items[i].UpdateQuality();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,9 @@
|
||||
|
||||
namespace csharp.StrategyPatternExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface that every category strategy will have to implement.
|
||||
/// </summary>
|
||||
interface ICategoryStrategy
|
||||
{
|
||||
void Update(Item item);
|
||||
|
||||
@ -3,6 +3,9 @@ using System.Collections.Generic;
|
||||
|
||||
namespace csharp.StrategyPatternExample
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapper class to allow to create a context for every defined item and thus to be able to bind a strategy.
|
||||
/// </summary>
|
||||
public class ItemWrapperContext
|
||||
{
|
||||
#region Variables
|
||||
|
||||
@ -3,6 +3,9 @@ using System.Collections.Generic;
|
||||
|
||||
namespace csharp.StrategyPatternExample.Strategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements the strategy; Increases in Quality as its SellIn value approaches.
|
||||
/// </summary>
|
||||
internal class CloseExpiredImproveQualityStrategy : ICategoryStrategy
|
||||
{
|
||||
#region subclasses
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace csharp.StrategyPatternExample.Strategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements the strategy; Do nothing. No action is implemented.
|
||||
/// </summary>
|
||||
internal class DoNothingStrategy : ICategoryStrategy
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public void Update(Item item)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,14 @@
|
||||
|
||||
namespace csharp.StrategyPatternExample.Strategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements the strategy; SellIn and Quality values are lowered.
|
||||
/// Main strategy for the most items.
|
||||
/// </summary>
|
||||
internal class NormalDegradeStrategy : ICategoryStrategy
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public void Update(Item item)
|
||||
{
|
||||
if (item.Quality > Global.MINIMUM_QUALITY)
|
||||
@ -16,7 +22,9 @@ namespace csharp.StrategyPatternExample.Strategy
|
||||
if (item.SellIn < 0 && item.Quality > Global.MINIMUM_QUALITY)
|
||||
{
|
||||
item.Quality--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,8 +2,13 @@
|
||||
|
||||
namespace csharp.StrategyPatternExample.Strategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements the strategy; Increase in Quality the older it gets.
|
||||
/// </summary>
|
||||
internal class OlderIsBetterStrategy : ICategoryStrategy
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public void Update(Item item)
|
||||
{
|
||||
if (item.Quality < Global.MAXIMUM_QUALITY)
|
||||
@ -18,5 +23,7 @@ namespace csharp.StrategyPatternExample.Strategy
|
||||
item.Quality++;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,8 +2,13 @@
|
||||
|
||||
namespace csharp.StrategyPatternExample.Strategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements the strategy; Items degrade in Quality twice as fast as normal items.
|
||||
/// </summary>
|
||||
internal class TwiceFastDegradeQualityStrategy : ICategoryStrategy
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public void Update(Item item)
|
||||
{
|
||||
int degrade = 2;
|
||||
@ -23,7 +28,9 @@ namespace csharp.StrategyPatternExample.Strategy
|
||||
if (item.Quality < Global.MINIMUM_QUALITY)
|
||||
{
|
||||
item.Quality = Global.MINIMUM_QUALITY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,6 +76,7 @@
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\CloseExpiredImproveQualityStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\NormalDegradeStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\DoNothingStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\OlderIsBetterStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\TwiceFastDegradeQualityStrategy.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user