聚合不同责任的最佳方式

发布于 2024-10-16 21:13:39 字数 860 浏览 2 评论 0原文

我有一个应用程序可以读取条形码、提取一部分并检查它是否有效。 我使用 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);
   }
}

I have an application that reads a barcode, extracts a part and checks if it is valid.
I use C#, Autofac and Nunit and I am undecided on which is the best implementation:

Solution A:
(Facade Pattern?)

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

   }
}

Solution B:
(Strategy Pattern?)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

流年已逝 2024-10-23 21:13:40

假设您的应用程序没有其他任何情况,请选择解决方案 A。它使事情尽可能简洁和可维护,直到您表明需要增加解决方案 B 的复杂性。第三种选择是甚至不使用构造函数,而是使用静态执行检查的函数。

此外,我会重命名你的函数。它们返回值,但它们的名称并不反映这一点。您想要使用 IsValid() 而不是 Check() 和 GetBarcode() 而不是 Read() 之类的东西。

Assuming nothing else about your app, go with Solution A. It keeps things as succinct and maintainable as possible, until you show a need for the added complexity of Solution B. A third option is to not even use the constructors, but to have static functions performing the check.

Additionally, I would rename your functions. They return values, but their names don't reflect that. You want to go with something like IsValid() instead of Check() and GetBarcode() instead of Read().

望喜 2024-10-23 21:13:40

我会选择您的解决方案 B。虽然它不一定看起来像策略模式,但我喜欢它,因为它可以更好地分离关注点。 BarcodeReader 是通用的,它只知道如何读取条形码的一部分并将其发回。它可用于任何读取条形码的应用程序。 Checker 看起来更具业务针对性。您的第一个解决方案是将业务逻辑和条形码的通用读取混合在一起。 (注意:我假设验证逻辑是业务逻辑,而不是特定于条形码的验证。如果是特定于条形码的验证,那么它应该进入条形码阅读器。)

我将在 XmlReader 之后对阅读器进行建模。它将检查格式良好的 xml 等。

I would opt for your Solution B. While it does not necessarily look like a strategy pattern I like it because it has better separation of concerns. The BarcodeReader is generic, it knows only how to read parts of a barcode and send it back. It can be used for any application reading barcodes. The Checker looks to be more business specific. Your first solution is mixing the business logic and the generic reading of barcodes together. (Note: I'm assuming that the logic for validating is business logic and not barcode specific validation. If it is barcode specific validation then it should go in the barcode reader.)

I would Model the reader after the XmlReader. It will check for wellformed xml, etc.

病女 2024-10-23 21:13:40

解决方案 B 使您的 Checker 类更易于测试,这通常表明您正在做正确的事情。 (您必须少存一个方法。)

如果您的消费类只对解析条形码感兴趣,那么这是一种更好的方法。

Solution B makes your Checker class easier to test, which is usually a sign that you're doing something right. (You have to stub one fewer method.)

If your consuming classes are only interested in the parsed barcode, that's a better approach.

甜心小果奶 2024-10-23 21:13:39

你是不是有点过度设计了?至少从示例来看是这样的。
我会放弃策略模式的想法。您是否会制定不止一种策略?

我喜欢第一个解决方案(良好的可测试性和 DI),但我不会将 3 行代码称为外观,真的。

Aren't you overengineering a bit? At least that's how it looks from the example.
I would drop the Strategy pattern idea. Are you going to ever have more then one strategy?

I like the first solution (good testability and DI), but I wouldn't call 3 line code a Facade, really.

鲸落 2024-10-23 21:13:39

我更喜欢解决方案 A,因为在我看来,它在分离关注点方面做得更好。 BarcodeReader 读取条形码并将其作为字符串返回(这就是条形码所代表的内容)。

I like solution A better because, in my opinion, it does a better job of separating concerns. The BarcodeReader reads a barcode and returns it as a string (which is what a barcode represents).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文