欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

设计模式学习一(简单工厂模式)

程序员文章站 2024-01-21 20:18:10
...

简单工厂模式


前言

简单工厂模式就是用一个单独的类创造实例的过程。

实例

以实现一个计算器为例学习简单工厂模式

1.面向过程的原代码

代码如下:

class Program
{
	void main()
	{
		std::cout<<"请输入数字A"<<std::endl;
		auto A;		
		std::cin>>A;
		std::cout<<"请选择运算符号+、-、*、/"<<std::endl;
		auto C;
		std::cin>>C;
		std::cout<<"请输入数字B"<<std::endl;
		auto B;
		auto D;
		std::cin>>B;
		
		if (C == '+')	D = A + B;
		if (C == '-')	D = A - B;
		if (C == '*')	D = A * B;
		if (C == '/')	D = A / B;
		std::cout<<"结果是:"<<D<<std::endl;
	}
}

代码存在的问题:
1、命名不规范
2、写法重复
3、未做异常处理

2.代码规范修改

代码如下:

class Program
{
	void main()
	{
		try
		{
			std::cout<<"请输入数字A"<<std::endl;
			auto NumberA;		
			std::cin>>NumberA;
			std::cout<<"请选择运算符号+、-、*、/"<<std::endl;
			auto Operate;
			std::cin>>Operate;
			std::cout<<"请输入数字B"<<std::endl;
			auto NumberB;
			auto Result;
			std::cin>>NumberB;
			switch(Operate)
			{
				case '+':
					Result = NumberA + NumberB; break;
				case '-':
					Result = NumberA - NumberB; break;
				case '*':
					Result = NumberA * NumberB; break;
				case '/':
					if (NumberB == 0)	throw NumberA;
					Result = NumberA / NumberB; break;
			}
			std::cout<<"结果是:"<<Result <<std::endl;
		}
		catch (int e){
		cout << e << " is divided by zero! " <<endl;
		}
	}
}

此代码仍为面向过程编程,如果你要在不同的系统上运行,虽然计算的方法一样,但是因为显示方法不同,代码复用性很低,因此要让计算和显示分开,代码封装,实现复用。

3.代码的封装

运算类Operation

class Operation
{
	public:
	auto GetResult(auto numberA, auto numberB ,auto operate)
	{
		auto result = 0.0;
		switch(operate)
			{
				case '+':
					result = numberA + numberB ; break;
				case '-':
					result = numberA - numberB ; break;
				case '*':
					result = numberA * numberB ; break;
				case '/':
					result = numberA / numberB ; break;
			}
			return result; 
	}
}

客户端代码

void main()
{
	try
	{
		std::cout<<"请输入数字A"<<std::endl;
		auto NumberA;		
		std::cin>>NumberA;
		std::cout<<"请选择运算符号+、-、*、/"<<std::endl;
		auto Operate;
		std::cin>>Operate;
		std::cout<<"请输入数字B"<<std::endl;
		auto NumberB;
		auto Result;
		std::cin>>NumberB;
		Operation Operation_object;
		if(Operate == '/' && NumberB == 0)  throw NumberA;
		auto Result = Operation_object.GetResult(NumberA,NumberB,Operate);
		std::cout<<"结果是:"<<Result <<std::endl;
	}
	catch (int e){
		cout << e << " is divided by zero! " <<endl;
	}
}

如果要加一个新的运算,需要让所有运算都参加编译,这种写法如果你要改算法,就必须把原代码交给你,风险太大,所以通过继承和多态来写。

4.继承修改

运算类Operation

class Operation
{
	private:
	auto m_numberA;
	auto m_numberB;
	public:
	auto GetNumberA()
	{
		return m_numberA;
	}
	void SetNumberA(auto numberA)
	{
		m_numberA = numberA;
	}
	auto GetNumberB()
	{
		return m_numberB;
	}
	void SetNumberB(auto numberB)
	{
		m_numberB = numberB;
	}
	virtual auto GetResult()
	{
		auto result = 0.0;
		return result;
	}
}

加减乘除类

class OperationAdd : public Operation
{
	public:
		auto GetResult()
		{
			auto result = GetNumberA() + GetNumberB();
			return result;
		}
}
class OperationSub : public Operation
{
	public:
		auto GetResult()
		{
			auto result = GetNumberA() - GetNumberB();
			return result;
		}
}
class OperationMul : public Operation
{
	public:
		auto GetResult()
		{
			auto result = GetNumberA() * GetNumberB();
			return result;
		}
}
class OperationDiv : public Operation
{
	public:
		auto GetResult()
		{
			auto result = 0.0;
			if(GetNumberB() == 0)	throw GetNumberB();
			result  = GetNumberA() / GetNumberB();
			return result;
		}
}

现在问题就是实例化对象

5.工厂类生产对象

工厂类

class OperationFactory
{
	public:
		Operation CreateOperate(auto operate)
		{
			Operation oper = null;
			switch (operate)
			{
				case '+':
					oper = new OperationAdd();
					break;
				case '+':
					oper = new OperationSub();
					break;
				case '+':
					oper = new OperationMul();
					break;
				case '+':
					oper = new OperationDiv();
					break;
			}
			return oper;
		}
}

客户端

int main()
{
	Operation oper;
	OperationFactory factory;
	oper = factory.CreateOperate('+');
	oper.SetNumberA(1);
	oper.SetNumberB(2);
	auto result = oper.GetResult();
}

如果需要增加新的运算,只要增加相应的运算子类,修改工厂类即可。且算法与界面分离,代码可移植可复用。


总结

简单工厂模式是为了使代码更加便于维护扩展和复用,考虑让代码更简洁,更具有普适性。

相关标签: 笔记 c++