Java implementation status (state) mode

Source: Internet
Author: User

/** * @author Stone */public class WindowState {private string Statevalue;public WindowState (String statevalue) {This.stat Evalue = Statevalue;} Public String Getstatevalue () {return statevalue;} public void Setstatevalue (String statevalue) {this.statevalue = Statevalue;}  public void handle () {/* * varies by state, then toggles state */if ("form". Equals (Statevalue)) {Switchwindow (); this.statevalue = "fullscreen";} else if ("Full Screen". Equals (Statevalue)) {switchfullscreen (); this.statevalue = "form";}} private void Switchwindow () {System.out.println ("Switch to form State");} private void Switchfullscreen () {System.out.println ("switch to full screen state");}}

/** * State of Use * @author Stone * */public class Windowcontext {private WindowState state;public windowcontext (WindowState STA TE) {this.state  = state;} Public WindowState GetState () {return state;} public void SetState (WindowState state) {this.state = state;} public void SwitchState () {This.state.handle ();}}

/* state mode Behavioral mode * Change the state of the object and change the behavior of the object * based on state, change behavior */public class Test {public static void main (string[] args) {/* * The state value of the example is only two, which is controlled by the State class itself * and the state value can be controlled by the client to set */windowcontext context = new Windowcontext (New WindowState ("form")); Context.switchstate (); Context.switchstate (); Context.switchstate (); Context.switchstate ();}}

Print

Toggle to form State toggle to full screen state toggle to full screen state




Java Implementation status (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.