"Design Mode Design Pattern" Reading Notes 18
State mode
structure
purpose
When an object (Context) is based on the status of the current itself, a state mode can be employed. Transfer all behaviors / motions related to the state of change to an external STATE object. In this way, it is possible to avoid determination of a large number of Switch.IFs within CONTEXT.
Favorable point
Bring greater flexibility and maintainability, because a new state can be easily appended by derived a class. Of course, the disadvantage is to increase the number of classes and reduce compactness.
discuss
1. If State does not have an internal state, it can be shared. At this time, it is a Flyweight without Intrinsic State.
2, the Transition Migration (Transition) If it is fixed, you can do it within Context. If more flexible, State's migration is made by each state. At this time, Context needs to provide a interface to set the state object to set the state of the Context; the advantage is to easily modify or extend the State migration logic by derived a new class. The shortcoming is A state at least you need to know a additional state, adding the coupling between State.
3, State objects can be created when needed, delete it after running; you can also prepare all State, never delete. Depending on the frequency of STATE migration, the reasonable selection is performed.
4, State object is generally Singletons.
Reference
"Design Pattern" Gang-Of-4 1997