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