欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-01-25 16:27:38
问题背景: 编程初学者都会有这样的问题,碰到问题就直觉地用计算机能够理解的逻辑来描述待解决的问题以及具体的求解过程,但这样的程序只为满足当前的需求,不容易维护和拓展。 问题解决思路: 设计一段代码之前,首先需要考虑到其可维护、可拓展性; 将面向对象特性充分融入到代码设计过程中; calculator ......

问题背景:

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

问题解决思路:

  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

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 }

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 }