第一章 代码无错就是优?简单工厂模式(读书笔记)

本文通过一个计算器的例子,详细介绍了简单工厂模式的应用。从最初的面向过程到面向对象的转变过程中,展示了如何利用简单工厂模式简化代码并提高程序的可维护性和扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一章 代码无错就是优?简单工厂模式

 

1.编程是一门技术,更加是一门艺术。
2.传统印刷术其实就是最早的面向对象(可重用)的范例。
3.传统印刷术的问题就在于所有的字都刻在同一版面上造成耦合度太高所致,开始用设计模式使得程序更加的灵活。容易修改,并且易于复用。
4.计算器的例子中让计算和显示分开,应该就是MVC中的解耦和。
5.UML:实现接口用空心三角形+虚线来表示。
6.聚合(Aggregation)表示一种弱的拥有关系。体现的是A对象可以包含B对象,但B对象不是A对象的一部分。聚合关系用空心的菱形+实线箭头来表示。
7.合成(Composition)是一种强的拥有关系,体现了严格的部分和整体的关系,部分和整体的生命周期一样。
8.加减乘除其实只会有一个被创建,所以封装他的创建过程,使程序简单化是非常必要的。简单工厂模式用在这里非常合适。

 -------------------------------------------------------------------------------------------------------------------------------------

程序1.1 最简单的面向过程的计算器

#include "stdafx.h"
#include <iostream>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "请输入数字A" << std::endl;
	std::string stringA;
	std::cin >> stringA;

	std::cout << "请输入符号+-*/" << std::endl;
	std::string stringC;
	std::cin >> stringC;

	std::cout << "请输入数字B" << std::endl;
	std::string stringB;
	std::cin >> stringB;

	int iAnswer;
	//运算
	if (stringC == "+")
		iAnswer = atoi(stringA.c_str()) + atoi(stringB.c_str());
	if (stringC == "-")
		iAnswer = atoi(stringA.c_str()) - atoi(stringB.c_str());
	if (stringC == "*")
		iAnswer = atoi(stringA.c_str()) * atoi(stringB.c_str());
	if (stringC == "/")
		iAnswer = atoi(stringA.c_str()) / atoi(stringB.c_str());

	std::cout << "结果是:" << iAnswer;
	return 0;
}

 

-------------------------------------------------------------------------------------------------------------------------------------

程序1.3 在上面程序的基础上,进行了优化,但还是面向过程的程序。

int _tmain(int argc, _TCHAR* argv[])
{
	try
	{
		std::cout << "请输入数字A" << std::endl;
		std::string stringA;
		std::cin >> stringA;

		std::cout << "请输入符号+-*/" << std::endl;
		char charC;
		std::cin >> charC;

		std::cout << "请输入数字B" << std::endl;
		std::string stringB;
		std::cin >> stringB;

		std::string iAnswer("");

		switch (charC)
		{
			case '+':
				iAnswer = atoi(stringA.c_str()) + atoi(stringB.c_str());
				break;
			case '-':
				iAnswer = atoi(stringA.c_str()) - atoi(stringB.c_str());
				break;
			case '*':
				iAnswer = atoi(stringA.c_str()) * atoi(stringB.c_str());
				break;
			case '/':
				if (atoi(stringB.c_str()) != 0 )
					iAnswer = atoi(stringA.c_str()) / atoi(stringB.c_str());
				break;
			default:
				break;
		}

	std::cout << "结果是:" << iAnswer;
	}
	catch(...)
	{
		std::cout << "您的输入有错";
	}
	
	return 0;
}

 

-------------------------------------------------------------------------------------------------------------------------------------

程序1.8 增加了业务的封装,代码更加简洁,但还不是面向对象。

#include "stdafx.h"
#include <iostream>
#include <string>

struct Operation
{
	static double GetResult(double numberA, double numberB,char myOperator)
	{
		double retVal;
		switch (myOperator)
		{
			case '+':
				retVal = numberA + numberB;
				break;
			case '-':
				retVal = numberA - numberB;
				break;
			case '*':
				retVal = numberA * numberB;
				break;
			case '/':
				retVal = numberA / numberB;
				break;
			default:
				break;
		}
		return retVal;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	try
	{
		std::cout << "请输入数字A" << std::endl;
		std::string stringA;
		std::cin >> stringA;

		std::cout << "请输入符号+-*/" << std::endl;
		char charC;
		std::cin >> charC;

		std::cout << "请输入数字B" << std::endl;
		std::string stringB;
		std::cin >> stringB;

		std::cout << "结果是:" << Operation::GetResult(atof(stringA.c_str()), atof(stringB.c_str()), charC);
	}
	catch(...)
	{
		std::cout << "您的输入有错";
	}
	return 0;
}

 

-------------------------------------------------------------------------------------------------------------------------------------

程序1.9 面向对象版的计算器--简单工厂模式

#include "stdafx.h"
#include <iostream>
#include <string>

//加减乘除的基类
class Operation
{
public:
	Operation()
		:m_numberA(0),m_numberB(0)
	{
	};
	Operation(double numberA, double numberB)
		:m_numberA(numberA),m_numberB(m_numberB)
	{
	};
	double getA()
	{
		return m_numberA;
	};
	void setA(double number)
	{
		m_numberA = number;
	};

	double getB()
	{
		return m_numberB;
	};
	void setB(double number)
	{
		m_numberB = number;
	};

	virtual double GetResult()
	{
		return 0;
	}
protected:
	//运算子A
	double m_numberA;
	//运算子B
	double m_numberB;
};
//加法类
class OperationAdd :public Operation
{
public:
	double GetResult()
	{
		double result = 0;
		result = m_numberA + m_numberB;
		return result;
	}
};
//减法类
class OperationSub :public Operation
{
public:
	double GetResult()
	{
		double result = 0;
		result = m_numberA - m_numberB;
		return result;
	}
};
//乘法类
class OperationMul :public Operation
{
public:
	double GetResult()
	{
		double result = 0;
		result = m_numberA * m_numberB;
		return result;
	}
};
//除法类
class OperationDiv :public Operation
{
public:
	double GetResult()
	{
		double result = 0;
		if ( m_numberB == 0 )
			throw "除数不能为0";
		result = m_numberA / m_numberB;
		return result;
	}
};

class OperationFactory
{
public:
	static Operation* createOperate(char oper)
	{
		Operation* myOper = NULL;
		switch (oper)
		{
		case '+':
			myOper = new OperationAdd();
			break;
		case '-':
			myOper = new OperationSub();
			break;
		case '*':
			myOper = new OperationMul();
			break;
		case '/':
			myOper = new OperationDiv();
			break;
		default:
			break;

		};
		return myOper;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	int a;
	int b;
	std::cin >> a >> b;
	Operation* op = OperationFactory::createOperate('-');
	op->setA(a);
	op->setB(b);
	std::cout << op->GetResult() << std::endl;
	return 0;
}


图1.9 简单工厂模式的UML图


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值