I don't understand the definition of state mode.

topic description

allows an object to change its behavior when its internal state changes, and the object seems to modify its class

what do you mean? My understanding of the first half of the sentence is. When an object changes from one state to another, his behavior changes accordingly. I can"t understand the second half of the sentence at all. is there a great god who can explain it in detail?

Mar.25,2021
The

state mode is more like an agent, giving its own behavior to the state. So if you change the state, the object seems to have modified its own "class" to become another class.


this sentence probably means that the behavior of the object has changed and it looks as if it no longer belongs to the original class. In fact, it does not seem that in the state mode, the concrete implementation that context delegates the behavior to state is to execute it. When the state changes, that is, the state object that executes the action has changed, that is to say, state is no longer the object of the original implementation class. But the "object" here should refer to the context object, and the context object itself has not changed, so it is said that "seems to have changed its class."


needs some examples to understand, which can be very practical. For example, many collection classes in JDK have this feature.

allow an object to change its behavior when its internal state changes

The value returned by the

List object size () method varies depending on the number of objects contained in the List.

there may be a more appropriate example, which is all I can think of for the time being.


. The subject of the question is to bring out the multiple choice questions in the exam.
this metaphysical problem maps to the fact that everyone has his own opinion.
the saying on the Internet is that the behaviors under different objects are extracted separately and encapsulated in specific state classes, so that the environment class object can change its behavior when its internal state changes. The object seems to modify its class very much, but in fact it is realized by switching to different specific states.
in fact, you should first find a state mode demo to write a test, either CPP or java, will probably understand

Menu