This is the 8th day of my participation in Gwen Challenge

The state pattern

The behavior of state mode is determined by the state, and there are different behaviors in different states. It seems that state patterns are similar in structure to policy patterns, but they differ in purpose and concept. The behavior of state patterns is parallel and non-replaceable, while policy patterns are independent and interchangeable. In simple terms, objects in state mode are in internal structure. Each state object has a common abstract base class. In this mode, the state changes internally and its execution behavior changes internally. The policy pattern is external to the object, and the controller receives the desired policy scheme from outside and then performs the corresponding function. Conceptually, there are still differences between the two modes. State focuses on the implementation of the same method no longer belongs to the same process and result when the state changes, while strategy focuses on the implementation of the same method and the output of different results in the strategy scheme.

The ordinary way

In normal development, there will be multiple states in some business scenarios. When this happens, it is common to set the state flag bits, and then define a state static variable to represent each state. For example, powerState represents the operating state of the computer. You can set the operating state of the computer. When executing different methods of the computer in different states, you can use if-else to judge whether the state is in line with expectations and execute the corresponding program.

The following code logic is too coupled, with multiple responsibilities, and does not conform to design principles. Later changes in the state structure would involve changes to the entire class, which would not be desirable.

public class ComputerController{
    private final POWER_ON = 1;
    private final POWER_OFF = 2;
    
    private int powerState = 1;
    
    public void open(a){
        powerState = POWER_ON;
    }
    
    public void close(a){
        powerState = POWER_OFF;
        
    }
    
    public void playGame(a){
        if(powerState == POWER_ON){ runGame(); }}public void playMusic(a){
        if(powerState == POWER_ON){ runMusic(); }}public void awaitOpen(a){
        if(powerState == POWER_OFF){ timeOpen(); }}}Copy the code

State mode mode

Set up a PowerState abstract interface class that implements all methods in all states; Controller as an abstract Controller class can be set to different states; PowerOpenState implements the PowerState abstract class method, and then implements the capability in the corresponding state, where the capability awaitOpen does not belong to the state is an empty method. PowerCloseState only needs to implement awaitOpen in this state; ComputerController implements the Controller abstract class, which configures objects in different states and implements each method in that state. At this point, there are no redundant if-else judgments when configuring different states to execute different methods, and the logic becomes clear and understandable.

public interface PowerState{
    void playGame(a);
    void playMusic(a);
    void awaitOpen(a);
}

public interface Controller{
    void open(a);
    void close(a);
}

public PowerOpenState extends PowerState{
    @override 
    void playGame(a){
        runGame();
    }
    @override 
    void playMusic(a){
        runMusic();
    }
    @override 
    void awaitOpen(a){}}public PowerCloseState extends PowerState{
    @override 
    void playGame(a){}
    @override 
    void playMusic(a){}
    @override 
    void awaitOpen(a){ timeOpen(); }}public class ComputerController extends Controller{
    PowerState powerState;
    
    @override 
    void open(a){
       powerState = new PowerOpenState();
    }
    @override 
    void close(a){
       powerState = new PowerCloseState();
    }
    

    void playGame(a){
        powerState.playGame();
    }
 
    void playMusic(a){
        powerState.playGame();
    }

    void awaitOpen(a){ powerState.awaitOpen(); }}Copy the code

conclusion

The advantage of the state pattern is that the if-else behavior is implemented in polymorphic form for the same behavior in different states. In the implementation of if-else form, the degree of logical coupling is too high, and the later maintenance is tedious and error-prone, but it does not mean that if-else is a bad form. Specific problems should be analyzed, and the significance of using this design mode is to consider in what scenarios the state mode is a better solution.

Of course, the state pattern also has its drawbacks. It can produce useless methods and increase the number of system classes and objects.