16th Chapter State Mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.