通过继承实现状态模式(C++)

 注意:先做类的声明和抽象基类的声明

抽象基类的函数方法中引入类,具体方法在类的实现后面声明。

在抽象基类的子类的函数中可以调用类的成员函数。

#include <iostream>


using namespace std;


class Contex;


class state {
public:
 virtual void Handel( Contex* contex) = 0;
};



class Contex {
public:
	
	Contex(state* _state) :State(_state) {};

	void changeState( state* _state)
	{
		State = _state;
	}

	void showState()
	{
		if (State != nullptr)
		{
			State->Handel(this);
		}
	}


	void showNum()
	{
		cout << num << endl;
	}
private:
	state *State = nullptr;
	int num = 10;

};




class state1 :public state {
public:
	void Handel(Contex* contex) {
		cout << "状态1" << endl;
	}
};

class state2 :public state {
public:
	void Handel(Contex* contex) {
		cout << "状态2" << endl;
	}
};


class state3 :public state {
public:
	void Handel(Contex* contex) {
		cout << "状态3" << endl;
		contex->showNum();
	}
};


int main()
{
	state *myState1 = new state1();
	state *myState2 = new state2();
	Contex *contex = new Contex(myState1);

	contex->showState();

	contex->changeState(myState2);

	contex->showState();

	state* myState3 = new state3();
	contex->changeState(myState3);
	contex->showState();


	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的状态模式示例: ```C #include <stdio.h> // 状态接口 typedef struct State State; struct State { void (*handle)(State*); }; // 具体状态A typedef struct StateA StateA; struct StateA { State state; // 继承状态接口 int count; // 计数器 }; void StateA_handle(State* super) { StateA* self = (StateA*) super; // 获取自身 printf("这是状态A,当前计数:%d\n", self->count++); } StateA* StateA_new() { StateA* self = (StateA*) malloc(sizeof(StateA)); // 分配内存 self->state.handle = StateA_handle; // 设置状态处理函数 self->count = 0; // 初始化计数器 return self; } // 具体状态B typedef struct StateB StateB; struct StateB { State state; // 继承状态接口 int count; // 计数器 }; void StateB_handle(State* super) { StateB* self = (StateB*) super; // 获取自身 printf("这是状态B,当前计数:%d\n", self->count++); if (self->count == 3) { // 转移状态 free(self); // 释放内存 self = StateA_new(); // 创建新状态对象 } } StateB* StateB_new() { StateB* self = (StateB*) malloc(sizeof(StateB)); // 分配内存 self->state.handle = StateB_handle; // 设置状态处理函数 self->count = 0; // 初始化计数器 return self; } // 环境类 typedef struct Context { State* state; // 当前状态 } Context; Context* Context_new() { Context* self = (Context*) malloc(sizeof(Context)); // 分配内存 self->state = (State*) StateB_new(); // 初始化状态为B return self; } void Context_request(Context* self) { self->state->handle(self->state); // 调用当前状态的处理函数 } // 测试程序 int main() { Context* context = Context_new(); // 创建环境对象 for (int i = 0; i < 5; i++) { // 循环调用状态处理函数 Context_request(context); } free(context->state); // 释放内存 free(context); return 0; } ``` 运行结果如下: ``` 这是状态B,当前计数:0 这是状态B,当前计数:1 这是状态B,当前计数:2 这是状态A,当前计数:0 这是状态A,当前计数:1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值