State mode of design mode (C++)

Author: Zhai Tianbao Steven
Copyright statement: The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source

1. What is the state mode?

       The state pattern is a behavioral software design pattern. When the internal state of an object changes, its behavior also changes. Just like when playing a game, different buff states, characters will have different damage, skills, etc.

       When the conditional expression controlling the state of an object is too complicated, this mode is very suitable for transferring complex judgment logic to a series of classes representing different states, which can greatly simplify the logic.

       Advantages of the state pattern:

  1. Good encapsulation. The behavior of each state is encapsulated into a corresponding class.
  2. Easy maintenance. Reduces the occurrence of if else or switch statements, and is suitable for scenarios with complex conditional judgments.
  3. Good scalability. Adding status is more convenient.

      Disadvantages of state pattern:

  1. As the number of states increases, the number of classes will also increase, which has higher requirements for developers.
  2. When there are few states, the application state pattern can appear redundant.

2. Status mode

2.1 Structure diagram

       The client is the Main main function. The lights have different behaviors in different states, and the states can be switched.

2.2 Code example

       Scenario Description: The light turns off initially, turns it on, and turns it off again.

//State.h
/****************************************************/
#pragma once
#include <iostream>

using namespace std;

class Lamp;

// 抽象状态类
class State 
{
public:
	// 析构函数
	virtual ~State() {}

	// 操作函数
	virtual void handle(Lamp& context) = 0;

};

// 具体状态类-开
class StateOn : public State 
{
public:
	// 操作函数
	virtual void handle(Lamp& context);

};

// 具体状态类-关
class StateOff : public State 
{
public:
	// 操作函数
	virtual void handle(Lamp& context);

};

// 灯
class Lamp
{
public:
	// 构造函数,默认状态关闭
	Lamp() : m_state(new StateOff()){}

	// 析构函数
	~Lamp();

	// 设置状态
	void setState(State* state);

	// 请求
	void request();
	
private:
	State* m_state;
};
//State.cpp
/****************************************************/
#include "State.h"

// 操作函数
void StateOn::handle(Lamp& context) {
	cout << "当前状态:打开" << endl;
	cout << "执行操作:关闭" << endl;
	context.setState(new StateOff());
}

// 操作函数
void StateOff::handle(Lamp& context) {
	cout << "当前状态:关闭" << endl;
	cout << "执行操作:打开" << endl;
	context.setState(new StateOn());
}

// 析构函数
Lamp::~Lamp() {
	if (m_state) {
		delete m_state;
		m_state = nullptr;
	}
}

// 设置状态
void Lamp::setState(State* state) {
	if (m_state) {
		delete m_state;
		m_state = nullptr;
	}
	m_state = state;
}

// 请求
void Lamp::request() {
	m_state->handle(*this);
}

//main.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "State.h"

using namespace std;

int main() 
{
	Lamp lamp;
	lamp.request(); // 操作灯
	lamp.request(); // 操作灯

	return 0;
}

       The program results are as follows.

       Pay attention to the data from new, don't forget to delete.

3. Summary

       I try my best to express my understanding of the state mode with more popular words and intuitive code routines. There may be some things that are not thoughtful. If you have different opinions, welcome to the comment area to communicate! Hope my examples help you understand the state pattern better.

       If the article helps you, you can give me a like to let me know, I will be very happy ~ come on!

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/130257608