/** * @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