我有一个读取条形码、提取部分并检查其是否有效的应用程序。 我使用 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);
}
}
最佳答案
你是不是有点过度设计了?至少从示例中看是这样。 我会放弃策略模式的想法。您是否会制定不止一种策略?
我喜欢第一个解决方案(良好的可测试性和 DI),但我真的不会将 3 行代码称为 Facade。
关于c# - 聚合不同责任的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4969972/