Benefits: Localize behaviors related to a particular state and separate the behavior of different states.
A state pattern can be considered when the behavior of an object depends on its state, and it must change its behavior according to state at run time.
/*** Created by Hero on 16-4-4.*/ Public Abstract classState { Public Abstract voidhandle (context context);}/*** Created by Hero on 16-4-4.*/ Public classConcretestateaextendsState {@Override Public voidhandle (Context context) {System.out.println ("State A"); Context.setstate (Newconcretestateb ()); Context.request (); }}/*** Created by Hero on 16-4-4.*/ Public classConcretestatebextendsState {@Override Public voidhandle (Context context) {System.out.println ("State B--->the end State"); //context.setstate (New Concretestatea ()); }}/*** Created by Hero on 16-4-4.*/ Public classContext {PrivateState state ; Public voidrequest () {State.handle ( This); } PublicContext (state state) { This. State =State ; } PublicState GetState () {returnState ; } Public voidsetState (state state) { This. State =State ; }} Public classMain { Public Static voidMain (string[] args) {context context=NewContext (NewConcretestatea ()); Context.request (); }}
16th Chapter State Mode