It looks like you are asking if there is a “concrete useful” benefit for using a state design template, which I would say definitely, yes, especially if your application really depends heavily on the “state” of its objects. A popular canonical example is a video player, which is always in one state and can only go into different states depending on the state it is currently in (for example, it cannot stop if it is already stopped, but it can play, and it can rewind, etc.).
While this specific example can be controlled relatively easily with a few if / else / switch-type conditions (if (isStopped ()), play (), etc.), because there are not many states to solve when states or their transition rules begin to become more numerous or complex, the state picture absolutely becomes very valuable, since without it your code tends to accumulate elseifs like crazy, and over time everything becomes less clear and manageable.
So yes, in general, if you find that the behavior of your objects changes depending on their state (if isStopped () play () / elseif isPlaying () stop () / elseif (isBroken () fix ()), etc. d.), then yes, consider using a state template. It works a little more ahead, but is usually worth the effort, and I did it right. I doubt that you have noticed significant overhead just to use it.
Head First Design Patterns offers an excellent description of their haws and whys too - I highly recommend compiling this book pretty much anyone who writes object-oriented code.
Chris nunciato
source share