If-less code: is it just an intelligent call or is it especially useful?

My friend talks about these design methods regarding state transitions of an object (he is a Java guru, by the way), executed without the boolean myState member boolean myState , but declaring the member myState as an object that implements the same "owner" interface.

Well, I was too cryptic, so you can find a discussion here with code samples.

Personally, I am excited about the approach, as my friend explained to me the philosophy of this; I also think that it is pretty consistent in terms of design. By the way, my problems relate mainly to performance and memory usage, since you can include compile time and runtime in the game. Since I do not know the JIT compiler and JVM internals, I am curious to have a wider opinion.

What do you think about?

+7
java oop design-patterns
source share
5 answers

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.

+4
source share

I have to disagree - useful design patterns aside, this specific example is ridiculous busting:

  • 15-line class with an easy-to-understand process
  • became a 50-line class with a confusing purpose

I don’t see how this improvement is - it violates YAGNI 1 and ASAP 2 inflates the code and reduces efficiency (several instances created to complete the task when it is not required).

Like an intellectual exercise, slightly interesting. Like a programming habit, scary! ;-)


1 YANGI = You won’t need it

2 ASAP = as easy as possible

+5
source share

Status / strategy models are tested and implemented. They are not just intellectual exercises. If you're thinking about performance and memory usage right now, you're probably shooting in the foot. Get the code ... then the profile.

+2
source share

This is a very useful method.

You are not distributing if (myState) statements around your class.
You can expand the technique in many states and only change what you assign to the myState member.

As for performance and memory usage, wait while you have something to measure - maybe you need to try both methods and measure them when they work.

+1
source share

Yes, it works, and it's cool ... to the point (the point where it gets dumb). You can even use anonymous inner classes, if necessary, even enumerations.

0
source share

All Articles