Java – who defines state transitions in state patterns?

I understand that the state pattern can be used to simulate objects that change behavior according to States and various states. Context can encapsulate the context in a specific class representing the state interface What I don't know is how the national transition takes place in this model Do states know and decide who follows them, or which country context they will get next?

Solution

From GOF design pattern book (this is in the implementation part):

Who defines national transition? The state mode does not specify which participant defines the criteria for state transition If standards are fixed, they can be fully implemented in context However, it is usually more flexible and appropriate for the group of States to specify its successor states and when to proceed with the transition This requires adding an interface so that the context can explicitly set the current state of the context

By decentralizing the transformation logic in this way, you can easily modify or extend the logic by defining new state subclasses One disadvantage of decentralization is that a subclass of a country will have at least one other knowledge, which introduces implementation dependencies between subclasses

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
分享
二维码
< <上一篇
下一篇>>