重构(1)if-else

分为五个部分,分别是卫语句、条件合并、表驱动、简单工厂模式、策略模式;

1)卫语句

未优化的代码:

复制代码
int age = 30;
std::string major = "软件工程";
bool bIsChoosePassed = true;

bool isChoosePassed()
{
	bool bIsPassed = false;
	if (age < 35) //卫语句1
	{
		if (major == "软件工程") //卫语句2
		{
			if (bIsChoosePassed == true) //卫语句3
			{
				bIsPassed = true;
			}
			else
			{
				std::cout << "简历未通过筛选!\n";
			}
		}
		else
		{
			std::cout << "专业不相符!\n";
		}
	}
	else
	{
		std::cout << "年龄太大了!\n";
	}
	return bIsPassed;
}

优化的代码:

复制代码
bool isChoosePassed()
{
	bool bIsPassed = false;
	if (age >= 35)
	{
		std::cout << "年龄太大了!\n";
		return bIsPassed;
	}
	if (major != "软件工程")
	{
		std::cout << "专业不相符!\n";
		return bIsPassed;
	}
	if (bIsChoosePassed != true)
	{
		std::cout << "简历未通过筛选!\n";
		return bIsPassed;
	}

	bIsPassed = true;
	return bIsPassed;
}

2)条件合并

未优化的代码:

复制代码
//1.if-else 条件合并
std::string getUserInfo(std::string name, int id, std::string state, bool isActivated)
{
	if (name == "")
	{
		return "invalid data receive!\n";
	}
	if (id <= 0)
	{
		return "invalid data receive!\n";
	}
	if (state == "")
	{
		return "invalid data receive!\n";
	}
	if (!isActivated)
	{
		return "user status is not normal!\n";
	}
	if (state == "") 
	{
		return "user status is not normal!\n";
	}
	return "welcom" + name;
}

优化的代码:

复制代码
std::string getUserInfo(std::string name, int id, std::string state, bool isActivated)
{
	if (name == "" || id <= 0 || state == "")
	{
		return "invalid data receive!\n";
	}

	if (!isActivated || state == "")
	{
		return "user status is not normal!\n";
	}

	return "welcom" + name;
}

3)表驱动

未优化的代码:

优化的代码:

复制代码
//函数指针+表驱动
int add(int a, int b)
{
	return a + b;
}
int sub(int a, int b)
{
	return a - b;
}
int mul(int a, int b)
{
	return a * b;
}
int division(int a, int b)
{
	assert(b != 0);
	return a / b;
}
// 定义函数指针
using func = int (*)(int, int);

int main()
{
	int res = 0;
	// 这里也可以用map来代替函数指针数组,但是小数据量上实际效果可能还没switch-case快
	func table[4];
	table['+'] = &add;
	table['-'] = &sub;
	table['*'] = &mul;
	table['/'] = &division;
	clock_t startTimes = clock();
	for (int i = 0; i < 1000000; ++i)
	{
		res = table['+'](res, i);
		res = table['-'](res, i);
	}
	clock_t endTimes = clock();
	std::cout << "arrary table cost times : " << (endTimes - startTimes) / CLOCKS_PER_SEC << "ms." << std::endl;
	getchar();
	return 0;
}

4)简单工厂模式

未优化的代码:

优化的代码:

复制代码
#include <iostream>
#include <time.h>

class Oper
{
public:
	Oper()
	{

	}
	~Oper()
	{

	}

public:
	virtual int process(int a, int b) = 0;
};

class Add : public Oper
{
public:
	Add()
	{

	}
	~Add()
	{

	}

public:
	int process(int a, int b)
	{
		return a + b;
	}
};

class Subtraction : public Oper
{
public:
	Subtraction()
	{

	}
	~Subtraction()
	{

	}

public:
	int process(int a, int b)
	{
		return a - b;
	}
};
class multiplication : public Oper
{
public:
	multiplication()
	{

	}
	~multiplication()
	{

	}

public:
	int process(int a, int b)
	{
		return a * b;
	}
};
class division : public oper
{
public:
	division()
	{

	}
	~division()
	{

	}

public:
	int process(int a, int b)
	{
		assert(b != 0);
		return a / b;
	}
};

class Factory
{
public:
	Factory(char symbol): m_oper(nullptr)
	{
		switch (symbol)
		{
		case ' + ':
			m_oper = new Add();

			break;
		case ' - ':
			m_oper = new Subtraction();
			break;
		case ' * ':
			m_oper = new Add();
			break;
		case ' / ':
			m_oper = new Add();
			break;
		default:
			std::cout << "wrong symbol" << std::endl;
			break;
		}
	};
	~Factory() {};

public:

	int Calculator( int a, int b)
	{
		if (m_oper == nullptr)
		{
			return -1;
		}
		return m_oper->process(a, b);
	}

private:
	Oper* m_oper;
};


int main()
{
	int res = 0;
	Factory* factory = new Factory();
	return 0;
}

4)策略模式

未优化的代码:

优化的代码:

相关推荐
zzywxc7872 小时前
AI技术通过提示词工程(Prompt Engineering)正在深度重塑职场生态和行业格局,这种变革不仅体现在效率提升,更在重构人机协作模式。
java·大数据·开发语言·人工智能·spring·重构·prompt
说私域21 小时前
开源链动2+1模式与AI智能名片融合下的S2B2C商城小程序源码:重构大零售时代新生态
人工智能·重构·开源
暮色驶过苍茫1 天前
软件设计与重构
重构
Deepoch1 天前
移动机器人的认知进化:Deepoc大模型重构寻迹本质
重构
小庞在加油1 天前
《重构项目》基于Apollo架构设计的项目重构方案(多种地图、多阶段、多任务、状态机管理)
c++·重构·apollo架构
Deepoch1 天前
测量认知革命:Deepoc大模型如何重构示波器的存在形态
重构
渲染101专业云渲染1 天前
川翔云电脑:突破硬件极限,重构设计生产力范式
重构·电脑
MARS_AI_2 天前
云蝠智能 Voice Agent 落地展会邀约场景:重构会展行业的智能交互范式
人工智能·自然语言处理·重构·交互·语音识别·信息与通信
火山引擎开发者社区2 天前
火山引擎Data Agent全面上线售卖!以企业级数据智能体,重构数据应用范式
人工智能·重构·火山引擎