mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-22 18:01:07 +00:00
Code refactored to adapt it to Strategy Pattern.
This commit is contained in:
parent
649e42d256
commit
75d8793a82
@ -12,7 +12,16 @@ namespace csharp
|
|||||||
this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList<ItemWrapperContext>();
|
this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList<ItemWrapperContext>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void UpdateQuality()
|
public void UpdateQuality()
|
||||||
|
{
|
||||||
|
for (var i = 0; i < Items.Count; i++)
|
||||||
|
{
|
||||||
|
Items[i].UpdateQuality();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateQuality_old()
|
||||||
{
|
{
|
||||||
for (var i = 0; i < Items.Count; i++)
|
for (var i = 0; i < Items.Count; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
77
csharp/Strategy/CategoryStrategiesFactory.cs
Normal file
77
csharp/Strategy/CategoryStrategiesFactory.cs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace csharp.Strategy
|
||||||
|
{
|
||||||
|
class CategoryStrategiesFactory
|
||||||
|
{
|
||||||
|
#region Variables
|
||||||
|
|
||||||
|
static private CategoryStrategiesFactory _strategiesFactory = null;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructor
|
||||||
|
|
||||||
|
private CategoryStrategiesFactory() { }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
public static CategoryStrategiesFactory GetInstance()
|
||||||
|
{
|
||||||
|
if (_strategiesFactory == null)
|
||||||
|
{
|
||||||
|
_strategiesFactory = new CategoryStrategiesFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
return _strategiesFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public List<ICategoryStrategy> GetCategoryStrategies(Item item)
|
||||||
|
{
|
||||||
|
List<ICategoryStrategy> listCategoryStrategies = new List<ICategoryStrategy>();
|
||||||
|
|
||||||
|
if (item.Name == "Aged Brie")
|
||||||
|
{
|
||||||
|
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||||
|
{
|
||||||
|
new OlderIsBetterStrategy()
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (item.Name == "Backstage passes to a TAFKAL80ETC concert")
|
||||||
|
{
|
||||||
|
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||||
|
{
|
||||||
|
new NextExpiredImproveQualityStrategy()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (item.Name == "Sulfuras, Hand of Ragnaros")
|
||||||
|
{
|
||||||
|
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||||
|
{
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (item.Name == "Conjured Mana Cake")
|
||||||
|
{
|
||||||
|
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||||
|
{
|
||||||
|
new TwiceFastDegradeQualityStrategy()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||||
|
{
|
||||||
|
new NormalDegradeStrategy()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return listCategoryStrategies;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
csharp/Strategy/ICategoryStrategy.cs
Normal file
9
csharp/Strategy/ICategoryStrategy.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace csharp.Strategy
|
||||||
|
{
|
||||||
|
interface ICategoryStrategy
|
||||||
|
{
|
||||||
|
void Update(Item item);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,10 +1,18 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace csharp.Strategy
|
namespace csharp.Strategy
|
||||||
{
|
{
|
||||||
public class ItemWrapperContext
|
public class ItemWrapperContext
|
||||||
{
|
{
|
||||||
#region Variables
|
#region Variables
|
||||||
|
|
||||||
|
private Item _item;
|
||||||
|
private List<ICategoryStrategy> listCategoryStrategies;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
|
||||||
public string Name
|
public string Name
|
||||||
{
|
{
|
||||||
@ -24,9 +32,6 @@ namespace csharp.Strategy
|
|||||||
set { this._item.SellIn = value; }
|
set { this._item.SellIn = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
//private List<ICategoryStrategy> listCategoryStrategies;
|
|
||||||
private Item _item;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
@ -35,22 +40,21 @@ namespace csharp.Strategy
|
|||||||
{
|
{
|
||||||
this._item = item;
|
this._item = item;
|
||||||
|
|
||||||
//listCategoryStrategies = StrategiesFactory.GetInstance().GetCategoryStrategies(item);
|
listCategoryStrategies = CategoryStrategiesFactory.GetInstance().GetCategoryStrategies(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
//public void UpdateQuality()
|
public void UpdateQuality()
|
||||||
//{
|
{
|
||||||
// foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies)
|
foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies)
|
||||||
// {
|
{
|
||||||
// categoryStrategyItem.UpdateQuality(this);
|
categoryStrategyItem.Update(this._item);
|
||||||
// }
|
}
|
||||||
//}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
csharp/Strategy/NextExpiredImproveQualityStrategy.cs
Normal file
37
csharp/Strategy/NextExpiredImproveQualityStrategy.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace csharp.Strategy
|
||||||
|
{
|
||||||
|
class NextExpiredImproveQualityStrategy : ICategoryStrategy
|
||||||
|
{
|
||||||
|
public void Update(Item item)
|
||||||
|
{
|
||||||
|
item.SellIn--;
|
||||||
|
|
||||||
|
if (item.SellIn < 0)
|
||||||
|
{
|
||||||
|
item.Quality = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int inc = 1;
|
||||||
|
|
||||||
|
if (item.SellIn < 5)
|
||||||
|
{
|
||||||
|
inc = 3;
|
||||||
|
}
|
||||||
|
else if (item.SellIn < 10)
|
||||||
|
{
|
||||||
|
inc = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
item.Quality += inc;
|
||||||
|
|
||||||
|
if (item.Quality > 50)
|
||||||
|
{
|
||||||
|
item.Quality = 50;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
csharp/Strategy/NormalDegradeStrategy.cs
Normal file
22
csharp/Strategy/NormalDegradeStrategy.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace csharp.Strategy
|
||||||
|
{
|
||||||
|
class NormalDegradeStrategy : ICategoryStrategy
|
||||||
|
{
|
||||||
|
public void Update(Item item)
|
||||||
|
{
|
||||||
|
if (item.Quality > 0)
|
||||||
|
{
|
||||||
|
item.Quality--;
|
||||||
|
}
|
||||||
|
|
||||||
|
item.SellIn--;
|
||||||
|
|
||||||
|
if (item.SellIn < 0 && item.Quality > 0)
|
||||||
|
{
|
||||||
|
item.Quality--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
csharp/Strategy/OlderIsBetterStrategy.cs
Normal file
26
csharp/Strategy/OlderIsBetterStrategy.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace csharp.Strategy
|
||||||
|
{
|
||||||
|
class OlderIsBetterStrategy : ICategoryStrategy
|
||||||
|
{
|
||||||
|
public void Update(Item item)
|
||||||
|
{
|
||||||
|
if (item.Quality < 50)
|
||||||
|
{
|
||||||
|
item.Quality++;
|
||||||
|
}
|
||||||
|
|
||||||
|
item.SellIn--;
|
||||||
|
|
||||||
|
if (item.SellIn < 0 && item.Quality < 50)
|
||||||
|
{
|
||||||
|
item.Quality++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
29
csharp/Strategy/TwiceFastDegradeQualityStrategy.cs
Normal file
29
csharp/Strategy/TwiceFastDegradeQualityStrategy.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace csharp.Strategy
|
||||||
|
{
|
||||||
|
class TwiceFastDegradeQualityStrategy : ICategoryStrategy
|
||||||
|
{
|
||||||
|
public void Update(Item item)
|
||||||
|
{
|
||||||
|
int degrade = 1;
|
||||||
|
|
||||||
|
item.SellIn--;
|
||||||
|
|
||||||
|
if (item.SellIn < 0)
|
||||||
|
{
|
||||||
|
degrade = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.Quality > 0)
|
||||||
|
{
|
||||||
|
item.Quality -= degrade;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.Quality < 0)
|
||||||
|
{
|
||||||
|
item.Quality = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props')" />
|
||||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
@ -65,9 +66,15 @@
|
|||||||
<Compile Include="GildedRose.cs" />
|
<Compile Include="GildedRose.cs" />
|
||||||
<Compile Include="GildedRoseTest.cs" />
|
<Compile Include="GildedRoseTest.cs" />
|
||||||
<Compile Include="Item.cs" />
|
<Compile Include="Item.cs" />
|
||||||
|
<Compile Include="Strategy\CategoryStrategiesFactory.cs" />
|
||||||
|
<Compile Include="Strategy\ICategoryStrategy.cs" />
|
||||||
<Compile Include="Strategy\ItemWrapperContext.cs" />
|
<Compile Include="Strategy\ItemWrapperContext.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Strategy\NextExpiredImproveQualityStrategy.cs" />
|
||||||
|
<Compile Include="Strategy\NormalDegradeStrategy.cs" />
|
||||||
|
<Compile Include="Strategy\OlderIsBetterStrategy.cs" />
|
||||||
|
<Compile Include="Strategy\TwiceFastDegradeQualityStrategy.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
@ -77,6 +84,12 @@
|
|||||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||||
|
<PropertyGroup>
|
||||||
|
<ErrorText>Este proyecto hace referencia a los paquetes NuGet que faltan en este equipo. Use la restauración de paquetes NuGet para descargarlos. Para obtener más información, consulte http://go.microsoft.com/fwlink/?LinkID=322105. El archivo que falta es {0}.</ErrorText>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Error Condition="!Exists('packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props'))" />
|
||||||
|
</Target>
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
Other similar extension points exist, see Microsoft.Common.targets.
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
<Target Name="BeforeBuild">
|
<Target Name="BeforeBuild">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user