2018年9月29日 星期六

『SOLID 重構挑戰練習』3. 使用 物件導向設計的 多型 來重構程式碼

『SOLID 重構挑戰練習』3. 使用 物件導向設計的 多型 來重構程式碼


using System;
using System.Collections.Generic;
using System.Drawing;
namespace RefactorSwitchPolymorphism
{
public class StringToColor
{
public Color Transfer(string name)
{
Color transferResult;
switch (name.ToLower())
{
case "red":
transferResult = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
break;
case "green":
transferResult = Color.FromArgb(0xFF, 0x80, 0x80, 0x80);
break;
case "blue":
transferResult = Color.FromArgb(0xFF, 0x00, 0x00, 0xFF);
break;
default:
throw new ArgumentException("不正確的顏色名稱");
}
return transferResult;
}
}
namespace RefactorSwitchPolymorphism.Refactoring
{
#region 將不同顏色名稱轉換成為顏色物件的過程,設計成為不同的類別
public abstract class ColorTransfer
{
public abstract Color Transfer();
}
public class TransferToRed : ColorTransfer
{
public override Color Transfer()
{
return Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
}
}
public class TransferToGreen : ColorTransfer
{
public override Color Transfer()
{
return Color.FromArgb(0xFF, 0x80, 0x80, 0x80);
}
}
public class TransferToBlue : ColorTransfer
{
public override Color Transfer()
{
return Color.FromArgb(0xFF, 0x00, 0x00, 0xFF);
}
}
public class StringToColor
{
Dictionary<string, ColorTransfer> _colorMaps;
public StringToColor()
{
_colorMaps = new Dictionary<string, ColorTransfer>()
{
{"Red", new TransferToRed() },
{"Green", new TransferToGreen() },
{"Blue", new TransferToBlue() },
};
}
public Color Transfer(string name)
{
if (!_colorMaps.ContainsKey(name))
throw new ArgumentException("不正確的顏色名稱");
return _colorMaps[name].Transfer();
}
}
#endregion
}
class Program
{
static void Main(string[] args)
{
string MyColorName = "Blue";
Console.WriteLine("使用 Swith 來設計方法");
StringToColor foo = new StringToColor();
Color fooColor = foo.Transfer(MyColorName);
Console.WriteLine($"{MyColorName} = A:{fooColor.A} R:{fooColor.R} G:{fooColor.G} B:{fooColor.B}");
Console.WriteLine("Press any key for continuing...");
Console.ReadKey();
Console.WriteLine("使用 資料字典與多型 來重構方法,進行 Swith 需求設計");
RefactorSwitchPolymorphism.Refactoring.StringToColor fooRefactoring = new RefactorSwitchPolymorphism.Refactoring.StringToColor();
Color fooRefactoringColor = fooRefactoring.Transfer(MyColorName);
Console.WriteLine($"{MyColorName} = A:{fooRefactoringColor.A} R:{fooRefactoringColor.R} G:{fooRefactoringColor.G} B:{fooRefactoringColor.B}");
Console.WriteLine("Press any key for continuing...");
Console.ReadKey();
}
}
}


『SOLID 重構挑戰練習』2. 使用 資料字典 來重構程式碼

『SOLID 重構挑戰練習』2. 使用 資料字典 來重構程式碼


using System;
using System.Collections.Generic;
using System.Drawing;
namespace RefactorSwitchDictionary
{
public class StringToColor
{
public Color Transfer(string name)
{
Color transferResult;
switch (name.ToLower())
{
case "red":
transferResult = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
break;
case "green":
transferResult = Color.FromArgb(0xFF, 0x80, 0x80, 0x80);
break;
case "blue":
transferResult = Color.FromArgb(0xFF, 0x00, 0x00, 0xFF);
break;
default:
throw new ArgumentException("不正確的顏色名稱");
}
return transferResult;
}
}
namespace RefactorSwitchDictionary.Refactoring
{
#region 將不同顏色名稱轉換方法之表示式,透過資料字典來儲存
public class StringToColor
{
Dictionary<string, Color> ColorMaps;
public StringToColor()
{
ColorMaps = new Dictionary<string, Color>()
{
{"Red", Color.FromArgb(0xFF, 0xFF, 0x00, 0x00)},
{"Green", Color.FromArgb(0xFF, 0x80, 0x80, 0x80) },
{"Blue", Color.FromArgb(0xFF, 0x00, 0x00, 0xFF)}
};
}
public Color Transfer(string name)
{
if (!ColorMaps.ContainsKey(name))
throw new ArgumentException("不正確的顏色名稱");
return ColorMaps[name];
}
}
#endregion
}
class Program
{
static void Main(string[] args)
{
string MyColorName = "Blue";
Console.WriteLine("使用 Swith 來設計方法");
StringToColor foo = new StringToColor();
Color fooColor = foo.Transfer(MyColorName);
Console.WriteLine($"{MyColorName} = A:{fooColor.A} R:{fooColor.R} G:{fooColor.G} B:{fooColor.B}");
Console.WriteLine("Press any key for continuing...");
Console.ReadKey();
Console.WriteLine("使用 資料字典 來重構方法,進行 Swith 需求設計");
RefactorSwitchDictionary.Refactoring.StringToColor fooRefactoring = new RefactorSwitchDictionary.Refactoring.StringToColor();
Color fooRefactoringColor = fooRefactoring.Transfer(MyColorName);
Console.WriteLine($"{MyColorName} = A:{fooRefactoringColor.A} R:{fooRefactoringColor.R} G:{fooRefactoringColor.G} B:{fooRefactoringColor.B}");
Console.WriteLine("Press any key for continuing...");
Console.ReadKey();
}
}
}