Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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. –  AndreasScheinert Feb 20 '13 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 '13 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. –  AndreasScheinert Feb 21 '13 at 12:49
add comment

2 Answers

up vote 4 down vote accepted

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 iteration 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.

But if your code grows, however, it is not unlikely that you get 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 - this will lead straightforward to a pattern implementation looking similar to the diagram in your question.

share|improve this answer
    
thank you! this make it clear for me. –  panny Feb 19 '13 at 23:41
    
I would love to see That missing method would be the "context" you are looking for. emphasized –  panny Feb 19 '13 at 23:44
add comment

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.

share|improve this answer
    
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 '13 at 1:38
2  
@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 '13 at 3:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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