Java – if there is no code: is it just an intellectual challenge or is it useful?

A friend of mine is discussing these design techniques for object state transformation (he is a Java master, by the way). Instead of Boolean mystate member execution, mystate member is declared as an object that implements the same interface

Well, I'm too mysterious, so you can find the discussion here with code examples

Personally, I am very excited about this method because my friend explained the philosophy behind it to me; From a design point of view, I also think it is very coherent By the way, I'm concerned about performance and memory usage, because compile - time and run - time optimizations may get into the game Since I don't know the JIT compiler and the inside of the JVM, I'd like to know more opinions

What do you have in mind?

Solution

This sounds like asking whether there are "concrete and useful" benefits of using state design patterns. I'm sure, especially if your application actually depends heavily on the "state" of its objects A popular specification example is a video player, which is always in one state and can only switch to different states according to its current state (for example, if it has stopped but it can play, it can't stop, it can rewind, etc.)

Although a few if / else / switch type conditions (if (issstopped ()), play (), etc.) can be used to manage this particular example relatively easily, because there are not so many states to deal with. When the states or their transformation rules begin to become more or more complex, the state model definitely becomes very valuable, because without it, your code tends to accumulate other things like crazy, Over time, things become more and more unreadable and manageable

So yes, generally speaking, if you find that the behavior of your objects changes according to their state (if issstopped () play () / else if isplay () stop () / else if (isbroken () fix ()), etc.), then yes, please consider using state mode It's a lot more work, but it's usually worth working hard and doing it right. I doubt you'll find any significant overhead just to use it

Head first design patterns also provides a good description of its methods and reasons - I strongly recommend that this book be given to almost anyone writing object-oriented code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>