The best way to aggregate different responsibilities

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 have not decided what is best implemented on:

Solution A : (Facade template?)

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 Template?)

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);
   }
}
+3
source share
5 answers

Don't you recycle too much? At least, as it looks from an example. I would give up the idea of ​​a strategy template. Will you have more than one strategy?

I like the first solution (good testability and DI), but I would not call a 3-line code a facade, really.

0
source

. , , , . BarcodeReader - ( , -).

0

, A. , B. , , , .

, . , . - IsValid() Check() GetBarcode() Read().

0

B. , , . BarcodeReader , , - . - . Checker . - - - . (: , -, -. -, -.)

XmlReader. xml ..

0

Solution B simplifies class testing Checker, which is usually a sign that you are doing something right. (You must drown out another method.)

If your consumption classes are only interested in the analyzed barcode, this is the best approach.

0
source

Source: https://habr.com/ru/post/1791493/


All Articles