简单工厂模式
主要用于创建对象。新添加类时,不会影响以前的系统代码。核心思想是用一个工厂来 根据输入的条件产生不同的类,然后根据不同类的 virtual 函数得到不同的结果。
优点:适用于不同情况创建不同的类时 缺点:客户端必须要知道基类和工厂类,耦合性差 Opeartion.h
#ifndef CHAPTER1_OPERATION_H
#define CHAPTER1_OPERATION_H
#pragma once
#include <iostream>
class Operation{
public:
Operation();
virtual double GetResult();
void SetNumberA(double value);
void SetNumberB(double value);
double GetNumberA();
double GetNumberB();
protected:
double m_NumberA;
double m_NumberB;
};
#endif
Operation.cpp
#include "Operation.h"
Operation::Operation() :m_NumberA(0),m_NumberB(0){}
void Operation::SetNumberA(double value) {
this->m_NumberA = value;
}
void Operation::SetNumberB(double value) {
this->m_NumberB = value;
}
double Operation::GetNumberA(){
return m_NumberA;
}
double Operation::GetNumberB() {
return m_NumberB;
}
double Operation::GetResult() {
double result = 0;
return result;
}
OpeartionAdd.h
#ifndef CHAPTER1_OPERATIONADD_H
#define CHAPTER1_OPERATIONADD_H
#include "Operation.h"
class OperationAdd : public Operation{
public:
OperationAdd();
double GetResult() override;
};
#endif
OpeartionAdd.cpp
#include "OperationAdd.h"
OperationAdd::OperationAdd() {
std::cout << "operation of adding ..." << std::endl;
}
double OperationAdd::GetResult() {
double result = m_NumberA + m_NumberB;
return result;
}
OpeartionSub.h
#ifndef CHAPTER1_OPERATIONSUB_H
#define CHAPTER1_OPERATIONSUB_H
#include "Operation.h"
class OperationSub : public Operation{
public:
OperationSub();
double GetResult() override;
};
#endif
OpeartionSub.cpp
#include "OperationSub.h"
OperationSub::OperationSub() {
std::cout << "operation of subing ..." << std::endl;
}
double OperationSub::GetResult(){
double result = m_NumberA - m_NumberB;
return result;
}
OperationMul.h
#ifndef CHAPTER1_OPERATIONMUL_H
#define CHAPTER1_OPERATIONMUL_H
#include "Operation.h"
class OperationMul : public Operation{
public:
OperationMul();
double GetResult() override;
};
#endif
OperationMul.cpp
#include "OperationMul.h"
OperationMul::OperationMul() {
std::cout << "operation of muling ..." << std::endl;
}
double OperationMul::GetResult() {
double result = m_NumberA * m_NumberB;
return result;
}
OpeartionDiv.h
#ifndef CHAPTER1_OPERATIONDIV_H
#define CHAPTER1_OPERATIONDIV_H
#include "Operation.h"
class OperationDiv : public Operation{
public:
OperationDiv();
double GetResult() override;
};
#endif
OpeartionDiv.cpp
#include "OperationDiv.h"
OperationDiv::OperationDiv() {
std::cout << "operation of diving ..." << std::endl;
}
double OperationDiv::GetResult() {
if(m_NumberB==0){
std::cout << "除数不能为0!" << std::endl;
}
double result = m_NumberA / m_NumberB;
return result;
}
OpeartionFactory.h
#ifndef CHAPTER1_OPERATIONFACTORY_H
#define CHAPTER1_OPERATIONFACTORY_H
#include "Operation.h"
#include "OperationAdd.h"
#include "OperationSub.h"
#include "OperationMul.h"
#include "OperationDiv.h"
#include "string"
class OperationFactory{
public:
static Operation *CreatOperate(std::string operate);
};
#endif
OpeartionFactory.cpp
#include "OperationFactory.h"
Operation *OperationFactory::CreatOperate(std::string operate) {
Operation *oper;
if (operate == "+"){
oper = new OperationAdd();
}
else if (operate == "-"){
oper = new OperationSub();
}
else if (operate == "*"){
oper = new OperationMul();
}
else{
oper = new OperationDiv();
}
return oper;
}
main.cpp
#include <iostream>
#include "OperationFactory.h"
int main() {
std::string str[] = {"+","-","*","/"};
Operation *oper;
for (auto & i : str){
oper = OperationFactory::CreatOperate(i);
oper->SetNumberA(4);
oper->SetNumberB(2);
std::cout << oper->GetResult() << std::endl;
}
return 0;
}
|