every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog
0. 前言
商場收銀軟件為例
1. 基礎版
total = 0def click_ok(price,num):tot = price * numtotal += totprint('合計:', total)
增加打折
total = 0def click_ok(price,num,dis_num):tot = price * numtotal += totif dis_num == '0': # 正常收費passelif dis_num == '8':total *= 0.8elif dis_num == '7':total *= 0.7elif dis_num == '5':total *= 0.5print('合計:', total)
2. 簡單工廠
class cashSuper(ABC):"""收取現金超類"""@abstractmodedef acceptcash(money):"""接收現金抽象類"""passclass cashNormal(cashSuper):"""正常收費子類"""def acceptcash(money):return moneyclass cashRebate(cashSuper):"""打折收費子類"""def cash_rebate(rebate):"""打折"""self.rebate = rebatedef acceptcash(money):return money * self.rebateclass cashReturn(cashSuper):"""返利收費條件"""def cash_return(condition, moneyreturn):self.condition = condition # 返利條件self.moneyreturn = moneyreturndef accpetcash(money):if money >= self.condition:return money - (money-self.condition)*self.moneyreturnreturn money
class CashFactory:@staticmethoddef create_cash_accept(type_str) -> CashSuper:cs = Noneif type_str == "正常收費":cs = CashNormal()elif type_str == "滿 300 返 100":cr1 = CashReturn("300", "100")cs = cr1elif type_str == "打 8 折":cr2 = CashRebate("0.8")cs = cr2return cs
3. 策略模式
class cashSuper(ABC):"""收取現金超類"""@abstractmodedef acceptcash(money):"""接收現金抽象類"""passclass cashNormal(cashSuper):"""正常收費子類"""def acceptcash(money):return moneyclass cashRebate(cashSuper):"""打折收費子類"""def cash_rebate(rebate):"""打折"""self.rebate = rebatedef acceptcash(money):return money * self.rebateclass cashReturn(cashSuper):"""返利收費條件"""def cash_return(condition, moneyreturn):self.condition = condition # 返利條件self.moneyreturn = moneyreturndef accpetcash(money):if money >= self.condition:return money - (money-self.condition)*self.moneyreturnreturn money
class CashContext:def __init__(self, cash_super:cashSuper):self.cs = cash_super # 通過構造方法傳入具體的收費策略def get_result(self, money):return self.cs.accept_cash(money) # 根據收費策略計算結果
需要再客戶端判斷哪種收費方式,可進行如下修改
4. 策略和簡單工廠結合
這樣就不用再客戶端里面寫分類代碼了。
# 上下文類
class CashContext:def __init__(self, type: str):self.cs = Noneif type == "正常收費":self.cs = CashNormal()elif type == "滿 300 返 100":self.cs = CashReturn("300", "100")elif type == "打 8 折":self.cs = CashRebate("0.8")else:raise ValueError("無效的收費類型")def get_result(self, money: float) -> float:return self.cs.accept_cash(money)
5. 簡單工廠 VS 策略和簡單工廠結合
客戶端
簡單工廠
# 客戶端代碼
from cash_factory import CashFactory, CashSuper # 必須導入父類 CashSupertype = "滿 300 返 100"
money = 350# 客戶端需要顯式調用工廠,并知道返回的是 CashSuper 類型
csuper = CashFactory.create_cash_accept(type) # 客戶端知道 CashSuper 存在
result = csuper.accept_cash(money) # 客戶端需了解 accept_cash() 方法
策略和簡單工廠結合
# 客戶端代碼
from cash_context import CashContext # 僅需導入 CashContexttype = "滿 300 返 100"
money = 350# 客戶端只需與 CashContext 交互
csuper = CashContext(type) # 完全隱藏 CashSuper 和 CashFactory
result = csuper.get_result(money) # 統一方法名,隱藏具體實現
在簡單工廠中,工廠方法的返回值類型是 CashSuper,客戶端必須:
-
知道 CashSuper 是抽象父類
-
知道調用 accept_cash() 方法(如 csuper.accept_cash(money))
而策略+工廠模式通過 CashContext 封裝了工廠和策略的細節,客戶端只需調用統一的 get_result()。
小結:
-
簡單工廠的耦合體現在:客戶端需直接操作 CashSuper 的接口
-
策略+工廠的改進:通過 CashContext 屏蔽底層細節,使客戶端更純粹