C++小游戏 Who_Is_The_Suspect?

1000来行的C++小游戏,
最近写了两个这种项目了。比较尴尬的行数,因为设计模式的作用在这种规模下不明显,各种内存泄漏和耦合的问题也显现不出来。但是还是学到了一些东西的。
1.使用了单例模式,以前只写过Java的单例,C++因为静态变量赋值和Java不一样(必须在类外显式地带类型定义),所以单例模式也是不一样的;
2.使用 了C++11多线程和互斥量,用来实现NPC的自由走动,在不同回合锁住;
3.类图的设计有点问题,这到后面才发现,Location类的封装太弱了,各个类的职责不够明确,导致找bug的时候比较麻烦,可想而知如果后面再增加一两千行就更难维护;
4.边界问题没有界定好,导致总是跨界访问报错,这也是类设计的问题,没有在开始想好;
5.尽量使用了C++11的特性

项目链接:点这里 欢迎Star
刚开始画的类图如下:
在这里插入图片描述
我做完之后的项目目录:
在这里插入图片描述

单例模式的一个实现。

#include "Detective.h"
#include<string.h>

Detective* Detective::uniqueDetective = nullptr; //唯一的初始化方法
Detective::Detective(){}
Detective* Detective::getInstance(string name, string description) {
	if (uniqueDetective == nullptr) {
		uniqueDetective = new Detective();
		uniqueDetective->setName(name);
		uniqueDetective->setDescription(description);
	}
	return uniqueDetective;
}
#pragma once
#include <vector>

#include "Person.h"

/*
* @ClassName : Detective
* @Auther : treblez
* @E-Mail : treblez@126.com 
* @Date : 2020/11/04
* @Description : Use the singleton pattern to get the only Detective object
*/

class Detective :
	public Person
{
private:
	vector<string> note;
	static int a;
	static Detective* uniqueDetective;
	Detective();
public:
	static Detective* getInstance(string,string);
};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值