· Insert comments.

· DoNothingStrategy created.
This commit is contained in:
israel 2017-10-20 13:14:29 +01:00
parent e333daef74
commit f6db7aa935
11 changed files with 93 additions and 14 deletions

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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
}
}

View File

@ -2,6 +2,9 @@
namespace csharp.StrategyPatternExample
{
/// <summary>
/// Interface that every category strategy will have to implement.
/// </summary>
interface ICategoryStrategy
{
void Update(Item item);

View File

@ -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

View File

@ -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

View File

@ -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
}
}

View File

@ -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)
@ -18,5 +24,7 @@ namespace csharp.StrategyPatternExample.Strategy
item.Quality--;
}
}
#endregion
}
}

View File

@ -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
}
}

View File

@ -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;
@ -25,5 +30,7 @@ namespace csharp.StrategyPatternExample.Strategy
item.Quality = Global.MINIMUM_QUALITY;
}
}
#endregion
}
}

View File

@ -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>