码迷,mamicode.com
首页 > 其他好文 > 详细

《大话设计模式》之简单工厂模式

时间:2019-11-17 14:38:11      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:ios   define   lin   属性   技术   play   namespace   img   isp   

问题背景:

  编程初学者都会有这样的问题,碰到问题就直觉地用计算机能够理解的逻辑来描述待解决的问题以及具体的求解过程,但这样的程序只为满足当前的需求,不容易维护和拓展。

问题解决思路:

  1. 设计一段代码之前,首先需要考虑到其可维护可拓展性
  2. 面向对象特性充分融入到代码设计过程中;

 

calculator.h

技术图片
 1 #ifndef _CALCULATOR_H_
 2 #define _CALCULATOR_H_
 3 #include <string>
 4 
 5 struct Calculator {
 6     Calculator() = default;
 7     Calculator(double lNum, double rNum) 
 8         :leftNumber(lNum), rightNumber(rNum), result(0){}
 9     ~Calculator(){}
10 
11     virtual double getResult() = 0; //基类中可实现也可不实现
12 protected:
13     double leftNumber;
14     double rightNumber;
15     double result;
16 };
17 
18 /*
19 关于继承方式:
20     public     -- 属性不变
21     protected  -- public变成protected
22     private    -- 都变成private
23 */
24 class CalculatorAdd :public Calculator
25 {
26 public:
27     CalculatorAdd() = default;
28     CalculatorAdd(double lNum, double rNum) 
29         :Calculator(lNum, rNum){}
30     ~CalculatorAdd(){};
31     //
32     virtual double getResult();
33 };
34 class CalculatorSub :public Calculator
35 {
36 public:
37     CalculatorSub() = default;
38     CalculatorSub(double lNum, double rNum) 
39         :Calculator(lNum, rNum){}
40     ~CalculatorSub(){};
41     //
42     virtual double getResult();
43 };
44 class CalculatorMul :public Calculator
45 {
46 public:
47     CalculatorMul() = default;
48     CalculatorMul(double lNum, double rNum) 
49         :Calculator(lNum, rNum){}
50     ~CalculatorMul(){};
51     //
52     virtual double getResult();
53 };
54 class CalculatorDiv :public Calculator
55 {
56 public:
57     CalculatorDiv() = default;
58     CalculatorDiv(double lNum, double rNum)
59         :Calculator(lNum, rNum){}
60     ~CalculatorDiv(){};
61     //
62     virtual double getResult();
63 };
64 
65 struct FactoryCalculator {
66     FactoryCalculator() = default;
67     FactoryCalculator(std::string op, double num1, double num2);
68     double getResult(){ return p->getResult(); }//简短函数默认inline
69 private:
70     std::string operate;
71     double number1;
72     double number2;
73     Calculator *p;
74 };
75 
76 #endif
View Code

calculator.cpp

技术图片
 1 #include "calculator.h"
 2 #include <iostream>
 3 
 4 using namespace std; //通常,using只出现在源文件中,而不出现在头文件中
 5 
 6 double Calculator::getResult()
 7 {
 8     cout << "message print in Calculator::getResult" << endl;
 9     return 0.0;
10 }
11 
12 double CalculatorAdd::getResult()
13 {
14     Calculator::getResult();//我曾经来过
15 
16     result = leftNumber + rightNumber;
17     return result;
18 }
19 double CalculatorSub::getResult()
20 {
21     Calculator::getResult();
22 
23     result = leftNumber - rightNumber;
24     return result;
25 }
26 double CalculatorMul::getResult()
27 {
28     Calculator::getResult();
29 
30     result = leftNumber * rightNumber;
31     return result;
32 }
33 double CalculatorDiv::getResult()
34 {
35     Calculator::getResult();
36 
37     if (rightNumber == 0){
38         cout << "division cannot be zero" << endl;
39         return -1;
40     }
41     result = leftNumber / rightNumber;
42     return result;
43 }
44 
45 //如果函数体太长,则初始化列表就写在源文件(初始化列表必须跟函数体)
46 FactoryCalculator::FactoryCalculator(string op, double num1, double num2) 
47     :operate(op), number1(num1), number2(num2)
48 {
49     char flag = operate[0];
50     switch (flag)
51     {
52     case +:
53         p = new CalculatorAdd(number1, number2);
54         break;
55     case -:
56         p = new CalculatorSub(number1, num2);
57         break;
58     case *:
59         p = new CalculatorMul(number1, number2);
60         break;
61     case /:
62         p = new CalculatorDiv(number1, number2);
63         break;
64     default:
65         break;
66     }
67 }
View Code

main.cpp

技术图片
 1 #include "calculator.h"
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     FactoryCalculator *factoryCalculaotr = new FactoryCalculator("+", 1, 2);
 9     FactoryCalculator *factoryCalculaotr2 = new FactoryCalculator("-", 1, 2);
10     FactoryCalculator *factoryCalculaotr3 = new FactoryCalculator("*", 1, 2);
11     FactoryCalculator *factoryCalculaotr4 = new FactoryCalculator("/", 1, 2);
12     cout << factoryCalculaotr->getResult() << endl;
13     cout << factoryCalculaotr2->getResult() << endl;
14     cout << factoryCalculaotr3->getResult() << endl;
15     cout << factoryCalculaotr4->getResult() << endl;
16 
17     system("pause");
18 }
View Code

 

《大话设计模式》之简单工厂模式

标签:ios   define   lin   属性   技术   play   namespace   img   isp   

原文地址:https://www.cnblogs.com/orejia/p/11876000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!