23 Java Design Patterns of the "behavioral mode" state mode

Source: Internet
Author: User



State Interface Class

public interface State {void handle ();}

Bookedstate class

/** * Booked Status * @author Administrator * */public class Bookedstate implements state {@Overridepublic void handle () {SYSTEM.O Ut.println ("Room has been booked! Nobody else can fix it! ");}}
Checkedinstate class

/** * check-in Status * @author Administrator * */public class Checkedinstate implements state {@Overridepublic void handle () {Syste M.out.println ("Room has been checked in!" Please do not Disturb! ");}}
Freestate class

/** * Idle Status * @author Administrator * */public class Freestate implements state {@Overridepublic void handle () {System.out. println ("Room is FREE!!! No One lives! ");}}
Homecontext class

/** * Room Object * @author Administrator * */public class Homecontext {//If it is a banking system, this context class is an account. Switch between different statuses depending on the amount! Private State state;public void SetState (s) {System.out.println ("Modified status! ") state = S;state.handle ();}}
Client Test class

public class Client {public static void main (string[] args) {Homecontext ctx = new Homecontext (); Ctx.setstate (New Freestat E ()); Ctx.setstate (new Bookedstate ());}}






23 Java Design Patterns of the "behavioral mode" 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.