C++设计模式-状态模式案例(用户每日充值获取不同的VIP状态)

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++设计模式-状态模式案例(用户每日充值获取不同的VIP状态)相关的知识,希望对你有一定的参考价值。

这个状态模式在2019年07月05号写过一篇,当时是把大话设计模式中C#的代码翻译成C++的。

今天(2022年12月07日)需要用到这个,在此温习下,写个自己的案例出来。

这里的案例主要是这样的。

模拟2周(14天),有个用户类。用户需要每日充值,从而获取不同的VIP状态。

不仅仅有VIP状态,还有个金币。每日首充可以获得10个金币,充得多获得20个金币,不充值扣5个金币。

程序运行截图如下:

代码如下:

State.h

#pragma once

#include <string>
using namespace std;

class VIPState;

class People

public:
	People();
	~People();
	void addGold(const long &value);
	const long& getCurrentGold();
	void payByDay(const int &type);	//每日一充
	string currentStateDesc();

protected:
	void setState(VIPState *vipState);

private:
	long m_gold;
	VIPState *m_state;
;

People.h

#pragma once

#include <string>
using namespace std;

class People;

class VIPState

public:
	VIPState(People *people);

	virtual void vipIncreasing() = 0;
	virtual string getStatusDesc() const  = 0;
	virtual ~VIPState() = 0;

protected:
	People *m_people;
;


class SSSVIPState : public VIPState

public:
	SSSVIPState(People *people);
	~SSSVIPState();
	string getStatusDesc() const;
	void vipIncreasing();
;

class SVIPState : public VIPState

public:
	SVIPState(People *people);
	~SVIPState();
	string getStatusDesc() const;
	void vipIncreasing();
;

class CommonVIPState : public VIPState

public:
	CommonVIPState(People *people);
	~CommonVIPState();
	string getStatusDesc() const;
	void vipIncreasing();
;

State.cpp

#include "State.h"
#include "People.h"
#include <iostream>
using namespace std;


VIPState::VIPState(People *people)

	this->m_people = people;


VIPState::~VIPState() 



SSSVIPState::SSSVIPState(People *people) : VIPState(people)



SSSVIPState::~SSSVIPState()



string SSSVIPState::getStatusDesc() const

	return string("SSS级VIP");


void SSSVIPState::vipIncreasing() 

	m_people->addGold(100);
	cout << "金币+100" << endl;


SVIPState::SVIPState(People *people) : VIPState(people) 



SVIPState::~SVIPState()



string SVIPState::getStatusDesc() const

	return string("S级VIP");


void SVIPState::vipIncreasing()

	m_people->addGold(10);
	cout << "金币+10" << endl;



CommonVIPState::CommonVIPState(People *people) : VIPState(people) 



CommonVIPState::~CommonVIPState()



string CommonVIPState::getStatusDesc() const

	return string("普通VIP");


void CommonVIPState::vipIncreasing()

	long currentGold = m_people->getCurrentGold();
	if (currentGold <= 0)
		return;

	long dGold = currentGold - 5;
	if (dGold < 0) 

		m_people->addGold(currentGold);
		cout << "金币-" << currentGold << endl;
		return;
	
	
	m_people->addGold(-5);
	cout << "金币-5" << endl;

People.cpp

#include "People.h"
#include "State.h"
#include <iostream>
using namespace std;


People::People()

	cout << "People::People() called" << endl;
	this->m_state = nullptr;
	this->m_gold = 0;



People::~People()

	cout << "People::~People() called" << endl;
	if (this->m_state)
		delete this->m_state;


void People::addGold(const long & value)

	this->m_gold += value;


const long& People::getCurrentGold()

	return m_gold;


void People::payByDay(const int & type)

	if (type == 0) 

		this->setState(new CommonVIPState(this));
	
	else if (type == 1) 

		this->setState(new SVIPState(this));
	
	else 

		this->setState(new SSSVIPState(this));
	
	this->m_state->vipIncreasing();


string People::currentStateDesc()

	return this->m_state->getStatusDesc();


void People::setState(VIPState * vipState)

	if (this->m_state != nullptr) 

		delete this->m_state;
		m_state = nullptr;
	

	this->m_state = vipState;

main.cpp

#include <iostream>
#include <time.h>
#include "People.h"
using namespace std;

//每日一充

int main(int argc, char *argv[]) 

	srand((unsigned int)time(NULL));

	People *people = new People;

	for (int i = 0; i < 14; i++) 

		cout << "第 " << i + 1 << " 天";
		int num = rand() % 10;				//0~9的随机数
		if (num < 3) 						//不充值

			cout << "不充值" << endl;
			people->payByDay(0);
		
		else if (num >= 3 && num <= 6) 	//充个SVIP

			cout << "每日首充" << endl;
			people->payByDay(1);
		
		else 								//充个SSSVIP

			cout << "每日648" << endl;
			people->payByDay(2);
		

		cout << "当前 【" << people->currentStateDesc() << "】 " << "金币 【" << people->getCurrentGold() << "】" << endl;
		cout << "---------------------------" << endl;
	

	delete people;
	getchar();

	return 0;

源码打包下载地址:

CAndCPP/CppStateDemo at master · fengfanchen/CAndCPP · GitHub

 

以上是关于C++设计模式-状态模式案例(用户每日充值获取不同的VIP状态)的主要内容,如果未能解决你的问题,请参考以下文章

C++设计模式-状态模式案例(用户每日充值获取不同的VIP状态)

C++状态模式详解--设计模式(15)

两种语言实现设计模式(C++和Java)(十六:状态模式)

C++中的单例模式

行为型设计模式-状态模式

行为型设计模式-状态模式