《大話設計模式》之簡單工廠模式

問題背景:ios

  編程初學者都會有這樣的問題,碰到問題就直覺地用計算機可以理解的邏輯來描述待解決的問題以及具體的求解過程,但這樣的程序只爲知足當前的需求,不容易維護和拓展。編程

問題解決思路:ide

  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.cppspa

 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
相關文章
相關標籤/搜索