我有一個應用程序讀取條形碼,提取零件並檢查它是否有效。 我使用C#,Autofac和NUnit,我還在猶豫哪個是最好的實現:聚合不同責任的最佳方式
解決方案A:(?門面模式)
public class Checker {
public Checker(IBarcodeReader reader, IBarcodeParser parser) {
...
}
public bool Check() {
string barcode = reader.Read();
string id = parser.Parse(barcode);
// check if id is valid
}
}
溶液B: (策略模式?)
public class Checker {
public Checker(IBarcodeReader reader) {
...
}
public bool Check() {
string id = reader.Read();
// check if id is valid
}
}
public class BarcodeReader: IBarcodeReader {
public BarcodeReader(IBarcodeParser parser) {
...
}
public string Read() {
string barcode = ... // read barcode from device
return parser.Parse(barcode);
}
}
在第二天,你給條形碼閱讀器一個條形碼解析器實例,然後使用 - 這不是戰略模式的特徵,對吧? – 2011-02-11 14:18:34