Revisiting Design Patterns - State Patterns

1. Background introduction

Recently, there is such a business requirement in the product that different activities are displayed at different times (here is not the execution time of the activity, but the execution time of the activity); the requirement still needs to be expanded
by N types of activities in the future, and the expansion must be achieved Reuse some properties and methods from previous activities.

Combined with such needs, we explored the state mode again, and suddenly became enlightened, so we summarized and shared.

2. Ideas & plans

  • 1. Introduction to state mode
  • 2. Class diagram of state mode
  • 3. Status Mode Codes
  • 4. Where the state mode can be optimized
  • 5. Project actual combat in state mode, after optimization

3. Process

1. Introduction to state mode

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

2. Class diagram of state mode

insert image description here

3. Status Mode Codes

package com.a7DesignPattern.a3BehaviorType.a09State;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 10:39
 */
public abstract class State {
    
    
    public abstract void Handle(Context context);
}

package com.a7DesignPattern.a3BehaviorType.a09State;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 10:42
 */
public class ConcreteStateA extends State{
    
    
    @Override
    public void Handle(Context context) {
    
    
        context.setState(new ConcreteStateB());
    }
}

package com.a7DesignPattern.a3BehaviorType.a09State;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 10:43
 */
public class ConcreteStateB extends State{
    
    

    @Override
    public void Handle(Context context) {
    
    
        context.setState(new ConcreteStateA());
    }
}

package com.a7DesignPattern.a3BehaviorType.a09State;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 10:39
 */
public class Context {
    
    
    private State state;

    Context(State state){
    
    
        this.state = state;
    }

    public State getState() {
    
    
        return state;
    }

    public void setState(State state) {
    
    
        this.state = state;
        System.out.println("当前状态"+state.getClass().getName());
    }

    public void Request(){
    
    
        state.Handle(this);
    };
}

package com.a7DesignPattern.a3BehaviorType.a09State;

/**
 * 功能描述:
 *
 * @Author:makang
 * @Date: 2021/5/29 10:44
 */
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Context context = new Context(new ConcreteStateA());
        context.Request();
        context.Request();
        context.Request();
        context.Request();
        context.Request();
    }
}

4. Where the state mode can be optimized

The transfer of subclasses is hard-coded in the subclasses, so if subsequent new subclasses modify existing subclasses, it does not conform to the principle of opening and closing

5. Project actual combat in state mode, after optimization

package com.a7DesignPattern.a3BehaviorType.a09State.project;

public class ContentGrain {
    
    
    
    String name;

    private ContentGrain nextContentGrain;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public ContentGrain getNextContentGrain() {
    
    
        return nextContentGrain;
    }

    public void setNextContentGrain(ContentGrain nextContentGrain) {
    
    
        this.nextContentGrain = nextContentGrain;
    }

    public void send(){
    
    
        System.out.println("当前对象所属的子类名字为:"+this.getClass().getName());
    }

}

package com.a7DesignPattern.a3BehaviorType.a09State.project;

public class Topic extends ContentGrain{
    
    

    public void dianzan(){
    
    
        System.out.println("我还有单独点赞的方法");
    }

    public void send(){
    
    
        super.send();
        dianzan();
    }
}

package com.a7DesignPattern.a3BehaviorType.a09State.project;

public class Practice extends ContentGrain{
    
    

    public void isOK(){
    
    
        System.out.println("这个活动是有标准答案的");
    }

    public void send(){
    
    
        super.send();
        isOK();
    }

}

package com.a7DesignPattern.a3BehaviorType.a09State.project;

import java.util.List;

public class RunLink {
    
    
    private static ContentGrain contentGrain;

    static {
    
    
        //这里的链将在配置文件中进行构建
        ContentGrain contentGrain1 = new Topic();
        contentGrain = contentGrain1;
        ContentGrain contentGrain2 = new Practice();
        contentGrain1.setNextContentGrain(contentGrain2);
        ContentGrain contentGrain3 = new Topic();
        contentGrain2.setNextContentGrain(contentGrain3);
    }

    public void run(){
    
    
       while (contentGrain.getNextContentGrain() != null){
    
    
           contentGrain.send();
           contentGrain = contentGrain.getNextContentGrain();
       }
    }

}

package com.a7DesignPattern.a3BehaviorType.a09State.project;

public class Client {
    
    
    public static void main(String[] args) {
    
    

        RunLink runLink = new RunLink();
        runLink.run();

    }
}

Four. Summary

1. There is a clearer boundary for the application of design pattern principles and specific patterns to implement software engineering
2. It will be clearer to understand the charm of the pattern in the scene
3. Combined with the type of design pattern, the scene to be solved should be understood again more clearly

Five, sublimation

When your cognition improves, you might as well look back, the charm and greatness of the design pattern will amaze you again

Guess you like

Origin blog.csdn.net/u013030601/article/details/131884884