设计模式(4)--对象行为(8)--状态

2023-12-29 11:35:22
1. 意图

? ? 允许一个对象在其内部状态改变时改变它的行为。

2. 三种角色

? ? 上下文环境(Context)、抽象状态(State)、具体状态(Concrete State)

3. 优点

? ? 3.1 将与特定状态相关的行为局部化,并且将不同状态的行为分割开来。

? ? 3.2 使得状态转换显式化。

? ? 3.3 State对象可被共享。

4. 缺点

? ? 4.1 增加了子类的数目

5. 相关模式

? ? 5.1 状态对象通常用享元模式实现

? ? 5.2 状态对象通常是单件

6. 代码示意(C++)
#pragma once
#include <iostream>
using namespace std;

class Context;

class State
{
public:
	virtual void Handle(Context* pContext) = 0;
protected:
	State() {}
	void ChangeState(Context* pContext, State* pState);
};

class ConcreteStateA :public State
{
	static ConcreteStateA* s_instance;
public:
	static State* Instance() {
		if (0 == s_instance) {
			s_instance = new ConcreteStateA;
		}
		return s_instance;
	}
	static void DelInstance() {
		delete s_instance;
	}
public:
	virtual void Handle(Context* pContext);
protected:
	ConcreteStateA() {}
};

class ConcreteStateB :public State
{
	static ConcreteStateB* s_instance;
public:
	static State* Instance() {
		if (0 == s_instance) {
			s_instance = new ConcreteStateB;
		}
		return s_instance;
	}
	static void DelInstance() {
		delete s_instance;
	}
public:
	virtual void Handle(Context* pContext);
protected:
	ConcreteStateB() {}
};

class Context
{
	State* m_pState;
public:
	Context() {
		m_pState = ConcreteStateA::Instance();
	}
	void Request() {
		if (0 != m_pState) {
			m_pState->Handle(this);
		}
	}
private:
	friend class State;
	void ChangeState(State* pState) {
		m_pState = pState;
	}
};

State.cpp:

#include "State.h"

ConcreteStateA* ConcreteStateA::s_instance = 0;
ConcreteStateB* ConcreteStateB::s_instance = 0;

void State::ChangeState(Context* pContext, State* pState) {
	pContext->ChangeState(pState);
}

void ConcreteStateA::Handle(Context* pContext) {
	cout << "ConcreteStateA handled" << endl;
	ChangeState(pContext, ConcreteStateB::Instance());
}

void ConcreteStateB::Handle(Context* pContext) {
	cout << "ConcreteStateB handled" << endl;
}
#include "State.h"
int main() {

	Context* pContext = new Context();
	pContext->Request();
	pContext->Request();

	delete pContext;

	return 0;
}

运行结果:

? 6.1 不同的ConcreteState,对应不同的处理方法(3.1)

? 6.2 状态对象比多个一般变量更明确,更容易理解和管理(3.2)

? 6.3 代码里的状态对象是单例共享的(3.3)

? 6.4 一个ConcreteState需要知道下一个状态是谁。

文章来源:https://blog.csdn.net/myepicure/article/details/134910143
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。