日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

[Python设计模式] 第1章 计算器——简单工厂模式

發布時間:2024/4/15 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Python设计模式] 第1章 计算器——简单工厂模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

github地址:https://github.com/cheesezh/python_design_patterns

寫在前面的話

""" 讀書的時候上過《設計模式》這一門課,當時使用的教材是程杰老師的《大話設計模式》,使用的語言是C#,學過課程之后初期深感面向對象思想的偉大,但是很少應用到實際開發中。后來我接觸了Python,現在工作中用到最多的也是Python,或許是因為Python的便利性,我寫的很多腳本/程序都還是面向過程編程,缺少面向對象的思想在里邊。因此,我打算重讀程杰老師的《大話設計模式》并用Python進行實踐。""" by ZH奶酪——張賀

題目

用一種面向對象語言實現一個計算器控制臺程序, 要求輸入兩個數和運算符號(+-*/), 得到結果.

基礎版本

a = int(input("input a number:")) b = str(input("input a operater(+ - * /):")) c = int(input("input a number:"))if b == "+":print(a+c) elif b == "-":print(a-c) elif b == "*":print(a*c) else b == "/":print(a/c) input a number:16 input a operater(+ - * /):* input a number:2 32

點評

  • 變量命名不規范
  • 無用的if條件判斷太多
  • 除法運算中未考慮第二個數字為0的情況
  • 改進版本1.0——規范代碼

    number_a = int(input("input a number:")) operator = str(input("input a operater(+ - * /):")) number_b = int(input("input a number:"))if operator == "+":print(number_a + number_b) elif operator == "-":print(number_a - number_b) elif operator == "*":print(number_a * number_b) elif operator == "/":if number_b != 0:print(number_a / number_b)else:print("With operator '/', the second number can not be zero.") else:print("Wrong operator.") input a number:12 input a operater(+ - * /):/ input a number:0 With operator '/', the second number can not be zero.

    點評

  • 沒有使用面向對象的思想
  • 只滿足當前需求, 不易維護, 不易擴展, 不易復用, 不夠靈活
  • 為什么活字印刷術能位列四大發明?主要是其方法的思想。

  • 文章改字方便, 可維護
  • 一個字可以重復使用, 可復用
  • 文章加字容易, 可擴展
  • 文章改版只需移動活字, 靈活性好
  • 復制?復用?

    如果做一個帶圖形化界面的計算器,上邊的代碼需要再寫一次。為了避免這樣,需要將業務邏輯與界面邏輯分開,降低耦合度。

    改進版本2.0——利用封裝解耦

    class Operation():def __init__(self):self.result = Nonedef get_result(self, number_a, number_b, operator):if operator == "+":self.result = number_a + number_belif operator == "-":self.result = number_a - number_belif operator == "*":self.result = number_a * number_belif operator == "/":if number_b != 0:self.result = number_a / number_belse:print("With operator '/', the second number can not be zero.")else:print("Wrong operator.")return self.resultnumber_a = int(input("input a number:")) operator = str(input("input a operater(+ - * /):")) number_b = int(input("input a number:"))operation = Operation() print(operation.get_result(number_a, number_b, operator)) input a number:12 input a operater(+ - * /):+ input a number:12 24

    點評

  • 僅僅用到了封裝, 還沒用到繼承和多態。
  • 緊耦合?松耦合?

    如果要支持一個開根號運算,上邊的代碼需要改動包括加減乘除在內的get_result函數,應該將加減乘除運算分離, 修改其中一個不影響其他的幾個。那么就需要定義一個Operator基類, 將get_result定義為虛函數,然后通過繼承和多態,分別實現加減乘除四個子類,每個子類中定義虛函數的實現邏輯。

    參考資料: Python中的多態與虛函數

    改進版本3.0——簡單工廠模式

    from abc import ABCMeta, abstractmethodclass Operation():__metaclass__ = ABCMetadef __init__(self):self.result = None@abstractmethoddef get_result(self):passclass AddOperation(Operation):def get_result(self, number_a, number_b):self.result = number_a + number_breturn self.resultclass SubOperation(Operation):def get_result(self, number_a, number_b):self.result = number_a - number_breturn self.resultclass MulOperation(Operation):def get_result(self, number_a, number_b):self.result = number_a * number_breturn self.resultclass DivOperation(Operation):def get_result(self, number_a, number_b):if number_b == 0:print("With operator '/', the second number can not be zero.")return self.resultself.result = number_a / number_breturn self.result

    如何實例化?——簡單工廠模式

    現在加減乘除的實現邏輯已經進一步隔離,之后即使增加一個開根號運算符,也和加減乘除無關。那么如何去實例化這些類呢?可以用簡單工廠模式。

    class OperationFactory():@classmethoddef create_operate(self, operator):oper = Noneif operator == "+":oper = AddOperation()elif operator == "-":oper = SubOperation()elif operator == "*":oper = MulOperation()elif operator == "/":oper = DivOperation()else:print("Wrong operator.")return oper

    通過上邊的簡單工廠,輸入運算符號,就可以實例化出對應的對象。下邊是客戶端的代碼。

    number_a = int(input("input a number:")) operator = str(input("input a operater(+ - * /):")) number_b = int(input("input a number:"))oper = OperationFactory.create_operate(operator) print(oper.get_result(number_a, number_b)) input a number:12 input a operater(+ - * /):- input a number:12 0

    點評

  • 業務邏輯與界面邏輯隔離,不關心是控制臺程序還是GUI程序
  • 不同運算邏輯隔離,一個運算符的增刪改操作不會影響其他運算
  • 面向對象思想的封裝,繼承,多態都有所體現
  • 易維護,易擴展,易復用
  • 轉載于:https://www.cnblogs.com/CheeseZH/p/9363197.html

    總結

    以上是生活随笔為你收集整理的[Python设计模式] 第1章 计算器——简单工厂模式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。