2018年9月29日 星期六

『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();
}
}
}

違反 ISP Interface Segregation Principle 介面隔離原則

違反 ISP Interface Segregation Principle 介面隔離原則

ISP Interface Segregation Principle 介面隔離原則 也是對於很多想要了解 SOLID 物件導向程式設計觀念與技能的人,相當困惑的。有人會說,要是我,才不會設計出這樣的程式碼,但是,現實情況是,產生這樣的問題,真的很常見,而且,會造成開發者的困擾,這怎麼說呢?
羅馬不是一天造成的,程式不是很快就可以寫出來的,當相同的程式經過多人的手,並且有著修改時間壓力下,往往會造成很多不可思議的程式碼,畢竟,這些程式碼都是人寫出來的。在底下的範例中,開發人員想要開發出個辦公室事務機器,因此,定義了許多功能,也造就了 IBusinessPrinters 這個介面,緊接著實作出 AllInOnePrinter 這個具體類別;可是,客戶想要開發出更具有價格競爭力的商品,因此,就想到只留下列印功能,把其他功能都移除,設計出一個超時尚的 SimplePrinter 印表機,另外,又想要移除傳真功能而已,造就出讓中小企業有能夠購買的 CopyPrinter 拷貝列印機。從底下的類別宣告,您看出了甚麼問題嗎?
public interface IBusinessPrinters
{
    void Scan();
    void Print();
    void Copy();
    void FaxSending();
    void FaxReceiving();
}
public class AllInOnePrinter : IBusinessPrinters
{
    public void Copy() { Console.WriteLine("拷貝中"); }
    public void FaxReceiving() { Console.WriteLine("傳真接收中"); }
    public void FaxSending() { Console.WriteLine("傳真發送中"); }
    public void Print() { Console.WriteLine("列印中"); }
    public void Scan() { Console.WriteLine("掃描中"); }
}
public class SimplePrinter : IBusinessPrinters
{
    public void Copy() { Console.WriteLine("錯誤!! 無此功能"); }
    public void FaxReceiving() { Console.WriteLine("錯誤!! 無此功能"); }
    public void FaxSending() { Console.WriteLine("錯誤!! 無此功能"); }
    public void Print() { Console.WriteLine("列印中"); }
    public void Scan() { Console.WriteLine("錯誤!! 無此功能"); }
}
public class CopyPrinter : IBusinessPrinters
{
    public void Copy() { Console.WriteLine("拷貝中"); }
    public void FaxReceiving() { Console.WriteLine("錯誤!! 無此功能"); }
    public void FaxSending() { Console.WriteLine("錯誤!! 無此功能"); }
    public void Print() { Console.WriteLine("列印中"); }
    public void Scan() { Console.WriteLine("掃描中"); }
}