12

I'm trying to understand the strategy pattern and asking myself: is the context class must-have or can I leave it out without compromising pattern's purpose ?

I was under the impression I needed some kind of switch for reading different types of files but didn't want to just hack something and later deal with refactoring (although, of course it always happens that code can be refactored but the idea was: try to be as smart as possible in the design beforehand...):

enter image description here

Image taken from wikimedia

Can the client delegate directly to the Strategy interface or is there something I just missed to understand about the context class?

interface Reader {
    // read information from file and fill data list field of Client
    readFile();
}
class ExcelReader implements Reader{ /* */ }
class PdfReader implements Reader{ /* */}

class Client{
    // strategic choice
    Reader r;

    // data list field
    List<Data> data;

    // Client Constructor
    public Client(){
        if(<file ends in .xls>)
            r = new ExcelReader();
        else
            r = new PdfReader();
        r.readFile();
    }
}

So, above depicted is the context class missing. Does the code adhere to the strategy pattern?

3
  • 1
    As another interesting/important point I would like to bring to your attention that the concept of type classes in functional languages is "simply" the strategy pattern with kinds en.wikipedia.org/wiki/Kind_(type_theory)). Both are just an implementation mechanism for ad-hoc polymorphism. Feb 20, 2013 at 19:43
  • Is this (far or less) related to Java 8 Project Lambda? The Wikipedia article is too dense for me to just understand at once but if these are part of the theoretical background for efficiently using upcoming features of Java (or programming at general) I will happily invest more time in it.
    – panny
    Feb 20, 2013 at 22:55
  • 1
    Very far but I would argue yes, type classes need. A programming language which support higher kinds. That would be the case for scala and Haskell. My point here was that (ad hoc) polymorphism is implemented differently and if you step back you can learn some insights on polymorphism in general. Feb 21, 2013 at 12:49

2 Answers 2

14

In your example, the code calling readFile is part of the Client constructor. That method is the "context" you are looking for. The strategy pattern does not need a "context class" literally, and at the first version of your code the strategy object (the "Reader" in your case) may reside just in a local variable. Especially when there is just one "strategic method" ("readFile") to be called.

However, if your codebase grows from one version to the next, it is not unlikely getting more and more "strategic" methods to be called, and the decision which strategy to apply and the performing of the "strategic methods" will happen at different times and at different places in your code. So you start to refactor them to keep the logic in one place. This will lead straightforward to an implementation looking similar to the diagram in your question.

0
5

Certainly. Patterns are just guidelines. You will still need to adapt and apply them correctly for the problem at hand. Personally, I rarely ever allow the strategy to be set at runtime; more often it is specified on construction or spun up in a factory.

Though it could also be argued that setStrategy is private and my injection is just using the pattern as shown.

2
  • Does this mean depicted Context class can be left out without compromising the pattern? Or put another way, is it ok when my client class is the depicted context class?
    – panny
    Feb 18, 2013 at 1:38
  • 6
    @panny - I hesitate to answer the question because it indicates you've missed the point of the answer, and indeed patterns altogether. The strategy pattern allows you to vary behavior by supplying different concrete implementations behind an interface. It is a concept, not a formula.
    – Telastyn
    Feb 18, 2013 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.