I'm looking into converting a big switch statement in my code into more manageable chunks using the State pattern.
I've been reading about it in the Design Patterns book, and also looking at a tutorials point example here.
The code in that example looks wrong to me, since the author is calling the State functionality from outside the Context. Is that correct?
It's my understanding that it is the context that should be the wrapper for the states, and State changes should most likely be handled from within each State object. Otherwise it kind of defeats the purpose right?
While googling to try find the Design Patterns book State pattern diagram below:
I came across this example, which is much more in line with my thinking. So am I correct in thinking the Tutorials Point example is incorrect, and the State should be changed either by the states themselves, or by the Object that holds the Context with a call to context.request()?
Is there a valid case for doing it like in the Tutorials Point example? I can't see it myself, if you do it that way you're just going to end up with another switch or if statement.
Aucun commentaire:
Enregistrer un commentaire