C++设计模式-继承与多态影响耦合性(最基础的简单工厂模式小实例)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/84194533

继承与多态影响耦合性(最基础的简单工厂模式小实例)

原理:
通过继承和虚函数的方式修改某个子类对应函数的功能;
通过简单工厂模式到底实例化谁;

如果要增加复杂的运算只有增加响应的子类,以及工厂的分支即可;

程序运行截图如下:

目录结构如下:

源码如下:

OperationFactory.h

#pragma once

#include "Operation.h"
#include "OtherOperation.h"
#include <iostream>
#include <string>
using namespace std;

class OperationFactory{
public:
	static Operation *createOperate(string operateStr){
		Operation *operation;

		if(operateStr == "+")
			operation = new OperationAdd;
		else if(operateStr == "-")
			operation = new OperationSub;
		else if(operateStr == "*")
			operation = new OperationMul;
		else if(operateStr == "/")
			operation = new OperationDiv;
		else 
			throw "The operation is wrong";

		return operation;
	}
};

Oeration.h

#pragma once
class Operation
{
public:
	double getNumberA();
	double getNumberB();

	void setNumberA(const double numberA);
	void setNumberB(const double numberB);

	virtual double getResult();
	virtual ~Operation();

//protected:
	double m_numberA;
	double m_numberB;
};

OtherOperation.h

#pragma once

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

class OperationAdd:public Operation{
public:
	double getResult() override{
		return m_numberA + m_numberB;
	}

	~OperationAdd(){
		cout << "~OperationAdd called!" << endl;
	}
};

class OperationSub:public Operation{
public:
	double getResult() override{
		return m_numberA - m_numberB;
	}

	~OperationSub(){
		cout << "~OperationSub called!" << endl;
	}
};

class OperationMul:public Operation{
public:
	double getResult() override{
		return m_numberA * m_numberB;
	}

	~OperationMul(){
		cout << "~OperationMul called!" << endl;
	}
};

class OperationDiv:public Operation{
public:
	double getResult() override{
		if(m_numberB == 0)
			throw "The divisor cannot be 0";

		return m_numberA / m_numberB;
	}

	~OperationDiv(){
		cout << "~OperationDiv called!" << endl;
	}
};

main.cpp

#include "OperatinFactory.h"

int main(){

	double num1, num2;
	string operStr;

	cout << "Please in put NumA:";
	cin >> num1;
	cout << "Please in put operation:";
	cin >> operStr;
	cout << "Please in put NumB:";
	cin >> num2;

	Operation *oper;

	try{

		oper = OperationFactory::createOperate(operStr);
		oper->setNumberA(num1);
		oper->setNumberB(num2);
		cout << "The result is " << oper->getResult() << endl;
		delete oper;
	}
	catch(const char *msg){
		cout << "The error : " << msg << endl;
	}

	system("pause");
	return 0;
}

Operation.cpp

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

double Operation::getNumberA()
{
	return m_numberA;
}

double Operation::getNumberB()
{
	return m_numberB;
}

void Operation::setNumberA(const double numberA)
{
	m_numberA = numberA;
}

void Operation::setNumberB(const double numberB)
{
	m_numberB = numberB;
}

double Operation::getResult()
{
	return 0.0;
}

Operation::~Operation()
{
	cout << "~Operation called" << endl;
}

UML类图如下:

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/84194533