DEV Community

Discussion on: What You Need To Know About The Helpful Strategy Pattern

brunobck profile image

Great post sir, I have some points though.
At run time the "ifs and elses" and "_extractors.FirstOrDefault(e => e.UseExtractor(fileExtension));" aproach would have not differences, right? Because all the latter line does, is to get the first "extrator" that first satisfies the condition by doing "ifs and elses" as well...
I think your code would take more advantage of interfaces if it was like this:

public string[] Extract(string filePath, ITextExtractor extractor)
    if(extractor != null)
        return extractor.ExtractContent(filePath);
        throw new Exception("unable to extract content");

To decide at run time what Strategy to use, you might use some parameterized Factory Method.
Let me know what you think please.

kylegalbraith profile image
Kyle Galbraith Author

This could certainly work but it loses the benefit of abstracting out the extractors. The idea is that each extractor knows what it can extract and it indicates that with the function that finds it.

This provides nice encapsulation by allowing the class that implements the interface to decide what it can extract.

Your idea would be quite different and more akin to the Factory pattern.