Java and Design Patterns-state mode

Source: Internet
Author: User

concept: state mode wraps the behavior of the object being studied in different state objects, each of which belongs to a subclass of an abstract state class. The intent of a state pattern is to change the behavior of an object as it changes its internal state, which means that different states correspond to different behaviors. The schematic class diagram for the state mode is as follows:

UML Class Diagrams: Context: Can be understood as a control class

State is the status interface

Concretestatea and Concretestateb can be considered as the implementation of the interface, that is, the specific state implementation class.

usage Scenario: The state of an object determines the behavior of an object and dynamically adjusts the behavior of the object at run time based on state.

There are complex if else judgments in the code, and these branch judgments are related to the state.

The actual scene: (1) TV switch state, the TV on the state can be carried out all kinds of operation, shutdown state, can not carry out various operations.

(2) Wi-Fi status, WiFi on the state can be connected to WiFi, the shutdown status is not allowed to operate.

(3) Login status, this in the development of more commonly used, generally in the system to achieve sharing and forwarding operations to determine the user's login status, if already logged in can be operated, otherwise prompt the user to log in.

Let's implement scenario 1.

First, create a status interface (corresponding to the state interface in the 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 (corresponding to Concretestatea in UML class diagrams):

Package Com.state.demo;public class Poweronstate implements Tvstate {/** * power on, the remote control button is valid */@Overridepublic void Nextchane L () {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 (corresponding to Concretestateb in UML class diagrams):

Package Com.state.demo;public class Poweroffstate implements Tvstate {/** * off, all buttons are invalid */@Overridepublic void Nextchane L () {System.out.println ("---------shutdown status not available, please start-----------------------");} @Overridepublic void Prechanel () {System.out.println ("---------shutdown status not available, please turn on-----------------------");} @Overridepublic void Turnup () {System.out.println ("---------shutdown status not available, please turn on-----------------------");} @Overridepublic void Turndown () {System.out.println ("---------shutdown status not available, please turn on-----------------------");}}


The next control class (corresponding to the context in the 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 that tests 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}}

Run the following example:

Here are some small logic problem, that is, in the state of the boot, the user again call the boot to be prompted, then we can add the following code in 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 make a judgment call before each method. Then run the test class 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}}


Run as follows:

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

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.