concept: state mode wraps the behavior of the object being studied in different state objects. Each state object belongs to a subclass of an abstract state class.
The intent of the state pattern is to have an object change in its internal state. The behavior is changed, which is the different behavior of different states. The schematic class diagram for the state mode is as follows:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>
UML Class Diagrams: Context: Can be understood as a control class
State is the status interface
Concretestatea and Concretestateb can feel the implementation of the interface, which is the detailed state implementation class.
usage Scenario: The state of an object determines the behavior of an object and dynamically adjusts the behavior of an object based on state at execution time.
There is a complex if else inference in the code. And these branching inferences are related to the state.
The actual scene: (1) TV switch status, TV on the status of the ability to carry out a variety of operations, shutdown state, can not carry out various operations.
(2) WiFi status, WiFi is enabled to connect to WiFi, the shutdown status is not allowed to operate.
(3) Login status, this is more frequently used in development. Generally in the system to achieve sharing and forwarding operations such as the first to infer the user's login status, if you have logged in can be operated. Otherwise, the user is prompted to log on.
Let's implement scenario 1.
First create a status interface (the state interface in the corresponding UML class diagram):
Package Com.state.demo;public interface Tvstate {/** * TV status interface, which provides four methods */void Nextchanel (); void Prechanel (); void turnup (); void Turndown ();}
The next two implementation classes (Concretestatea in the corresponding UML class diagram):
Package Com.state.demo;public class Poweronstate implements Tvstate {/** * boot status, the remote control button is valid */@Overridepublic void NEXTC Hanel () {System.out.println ("---------Next channel-----------------------");} @Overridepublic void Prechanel () {System.out.println ("---------previous channel-----------------------");} @Overridepublic void Turnup () {System.out.println ("---------volume increase-----------------------");} @Overridepublic void Turndown () {System.out.println ("---------Volume Reduction-----------------------");}}
Implementation Class 2 (Concretestateb in the corresponding UML class diagram):
Package Com.state.demo;public class Poweroffstate implements Tvstate {/** * off state. All button is invalid */@Overridepublic void Nextchanel () {System.out.println ("---------shutdown state is not available. Please start the-----------------------");} @Overridepublic void Prechanel () {System.out.println ("---------shutdown status not available, please turn on-----------------------");} @Overridepublic void Turnup () {System.out.println ("---------shutdown state is not available. Please start the-----------------------");} @Overridepublic void Turndown () {System.out.println ("---------shutdown state is not available. Please start the-----------------------");}}
Next control class (context in the corresponding UML class diagram):
Package Com.state.demo;public class Tvcontroller {tvstate tvstate=null;public void settvstate (Tvstate TvState) { This.tvstate = tvstate;} /** * boot */public void Turnontv () {settvstate (New poweronstate ()); System.out.println ("--------boot---------------");} /** * shutdown */public void Turnofftv () {settvstate (New poweroffstate ()); System.out.println ("--------off---------------");} /** * Next channel */public void Nextchanel () {Tvstate.nextchanel ();} public void Prechanel () {Tvstate.prechanel ();} public void Turnup () {tvstate.turnup ();} public void Turndown () {Tvstate.turndown ();}}
Finally, write a test class and test the above code:
Package Com.state.demo;public class TestClass {public static void main (string[] args) {Tvcontroller tvcontroller=new Tvco Ntroller ();//Create a Control class TVCONTROLLER.TURNONTV ();//First boot tvcontroller.nextchanel (); Tvcontroller.turndown (); TVCONTROLLER.TURNOFFTV ();//Shutdown tvcontroller.turndown ();//function is no longer available after shutdown}}
Perform the actual proportions as follows:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>
Here are some small logic problems, that is, in the state of being turned on, the user again call the boot to be prompted. In this case, we can add the following code to the control class.
Package Com.state.demo;public class Tvcontroller {private Boolean istvon=false; Tvstate tvstate=null;public void Settvstate (Tvstate tvstate) {this.tvstate = tvstate;} /** * boot */public void Turnontv () {if (!istvon) {istvon=true;settvstate (New poweronstate ()); System.out.println ("--------boot---------------");} Else{istvon=true; System.out.println ("The--------is turned on, do not press the---------------");}} /** * shutdown */public void Turnofftv () {if (Istvon) {istvon=false;settvstate (New poweroffstate ()); System.out.println ("--------off---------------");} Else{istvon=false; System.out.println ("-------has been turned off, do not press the---------------");}} /** * Next channel */public void Nextchanel () {Tvstate.nextchanel ();} public void Prechanel () {Tvstate.prechanel ();} public void Turnup () {tvstate.turnup ();} public void Turndown () {Tvstate.turndown ();}}
Make a boot ID, and infer it before each call to the method.
The test class is then performed again:
Package Com.state.demo;public class TestClass {public static void main (string[] args) {Tvcontroller tvcontroller=new Tvco Ntroller ();//Create a Control class TVCONTROLLER.TURNONTV ();//First boot TVCONTROLLER.TURNONTV ();//First boot tvcontroller.nextchanel (); Tvcontroller.turndown (); Tvcontroller.turnofftv ();//Shutdown TVCONTROLLER.TURNOFFTV ();//Shutdown tvcontroller.turndown ();// function is no longer available after shutdown}}
Perform such as the following:
Summary: The control class holds the system state, but the control class does not directly handle the behavior, and the behavior is implemented in the implementation class of the State;
User direct operation control class, direct and control class interaction, not directly operational state implementation class, so there is a separation of responsibilities, conducive to system maintenance.
Like friends please pay attention to me, thank you.
Java and Design Patterns-state mode