标签:简单工厂模式
业务逻辑.h
#pragma once class Operation { protected: double numberA; double numberB; double result; public: void setValue(double numberA, double numberB) { this->numberA = numberA; this->numberB = numberB; } virtual double getValue() = 0; }; class OperationAdd :public Operation { public: double getValue() { result = numberA + numberB; return result; } }; class OperationSub :public Operation { public: double getValue() { result = numberA - numberB; return result; } };
简单工厂.h
#pragma once #include "业务逻辑.h" class OperationFactory { public: static Operation* CreateOperation(char operate) { Operation *oper; switch (operate) { case ‘+‘: oper = new OperationAdd; return oper; break; case ‘-‘: oper = new OperationSub; return oper; break; } } };
界面逻辑.cpp
#include <iostream> using namespace std; #include "工厂.h" int main() { double numberA; double numberB; char operate; cout << "请输入a+b的式子:" << endl; cin >> numberA >> operate >> numberB; Operation *oper; oper = OperationFactory::CreateOperation(operate); oper->setValue(numberA, numberB); cout << "结果为" << oper->getValue() << endl; system("pause"); return 0; }
本文出自 “sophoso的cs学习之路” 博客,请务必保留此出处http://sophoso.blog.51cto.com/9442872/1565682
标签:简单工厂模式
原文地址:http://sophoso.blog.51cto.com/9442872/1565682