define

Allows an object to change its behavior when its internal state changes and the object appears to modify its class.

nature

Separate and select behaviors based on state

His role

  • State of affairs

    Defines interfaces for different processing according to different states

  • ConcreteState: ConcreteState

    Implement the state interface, representing each specific state.

  • Context (condition, Context, Context)

    A class that implements state handling

The sample code

Public interface TvState {public void nextChange(); public void prevChange(); public void turnUp(); public void turnDown(); } /** * implements TvState{@override public void nextChange() { System.out.println(" last channel "); } @override public void prevChange() {system.out.println (" next channel "); } @override public void turnUp() {system.out.println (" turnUp the volume "); } @override public void turnDown() {system.out.println (" turnDown "); Public implements TvState{@override public void nextChange() {} @override Public void prevChange() {} @override public void turnDown() {} @override public void turnDown() {}} /** * TV remote control */ public class TvController implements PowerController{ private TvState tvState; public void setTvState(TvState tvState) { this.tvState = tvState; } @Override public void powerOn() { setTvState(new PowerOnState()); System.out.println(" boot "); } @Override public void powerOff() { setTvState(new PowerOffState()); System.out.println(" shutdown "); } public void nextChange() { tvState.nextChange(); } public void prevChange() { tvState.prevChange(); } public void turnUp() { tvState.turnUp(); } public void turnDown() { tvState.turnDown(); } } public class Client { public static void main(String[] args){ TvController tvController = new TvController(); Tvcontroller.poweron (); tvController.nextChange(); // next channel tvController.turnup (); // turn up the volume // set the shutdown state tvController.poweroff (); TurnDown (); // turnDown the volume, tvcontroller.turndown (); }}Copy the code

The results

Turn on a channel turn up the volume turn offCopy the code

function

The act of separating states, by maintaining changes in states, calls different implementations of different states. State and behavior are related, and their relationship can be described as: State determines behavior.

Since the state is changed at runtime, the behavior also changes at runtime depending on the state. It seems that the same object behaves differently at different runtime times, as if the class were modified.

advantages

  • Simplify the application of logic control by encapsulating the handling of a state using a class of receipts.
  • Better separation of state and behavior, by setting the state class all interfaces, actions and state corresponding to the state, all the behaviors are associated with a specific state into an object, make application in control, only need to care about the state of switching, and don’t care about the state of the corresponding real processing.
  • Better scalability. The introduction of a public interface for state handling makes it very easy to extend the new state by adding a new implementation class that implements the public interface for state handling, and then setting the state to change to the new state where state maintenance takes place.
  • Display to switch state.

disadvantages

  • Each state corresponds to a state processing class, increasing the class file.

When to use

  • If an object’s behavior depends on its state, and it must change its behavior at run-time based on its state, use state mode.
  • If an operation has a large number of branch statements that depend on its state, use the state mode.