【设计模式】2.策略模式

发布于:2025-06-09 ⋅ 阅读:(16) ⋅ 点赞:(0)

every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog

0. 前言

商场收银软件为例

1. 基础版

total = 0

def click_ok(price,num):
    tot = price * num
    total += tot
    print('合计:', total)

增加打折

total = 0

def click_ok(price,num,dis_num):
    tot = price * num
    total += tot
    
    if dis_num == '0': # 正常收费
        pass
    elif dis_num == '8':
        total *= 0.8
    elif dis_num == '7':
        total *= 0.7
    elif dis_num == '5':
        total *= 0.5
    
    print('合计:', total)

2. 简单工厂

20250511215044

class cashSuper(ABC):
    """收取现金超类"""
    
    @abstractmode
    def acceptcash(money):
        """接收现金抽象类"""
        pass

class cashNormal(cashSuper):
    """正常收费子类"""

    def acceptcash(money):
        return money

class cashRebate(cashSuper):
    """打折收费子类"""

    def cash_rebate(rebate):
        """打折"""
        self.rebate = rebate
    
    def acceptcash(money):
        return money * self.rebate

class cashReturn(cashSuper):
    """返利收费条件"""
    def cash_return(condition, moneyreturn):
        self.condition = condition # 返利条件
        self.moneyreturn = moneyreturn
    
    def accpetcash(money):
        if money >= self.condition:
            return money - (money-self.condition)*self.moneyreturn
        return money
class CashFactory:
    @staticmethod
    def create_cash_accept(type_str) -> CashSuper:
        cs = None
        if type_str == "正常收费":
            cs = CashNormal()
        elif type_str == "满 300 返 100":
            cr1 = CashReturn("300", "100")
            cs = cr1
        elif type_str == "打 8 折":
            cr2 = CashRebate("0.8")
            cs = cr2
        return cs

3. 策略模式

20250511221305

class cashSuper(ABC):
    """收取现金超类"""
    
    @abstractmode
    def acceptcash(money):
        """接收现金抽象类"""
        pass

class cashNormal(cashSuper):
    """正常收费子类"""

    def acceptcash(money):
        return money

class cashRebate(cashSuper):
    """打折收费子类"""

    def cash_rebate(rebate):
        """打折"""
        self.rebate = rebate
    
    def acceptcash(money):
        return money * self.rebate

class cashReturn(cashSuper):
    """返利收费条件"""
    def cash_return(condition, moneyreturn):
        self.condition = condition # 返利条件
        self.moneyreturn = moneyreturn
    
    def accpetcash(money):
        if money >= self.condition:
            return money - (money-self.condition)*self.moneyreturn
        return 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 = None
        if 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  # 必须导入父类 CashSuper

type = "满 300 返 100"
money = 350

# 客户端需要显式调用工厂,并知道返回的是 CashSuper 类型
csuper = CashFactory.create_cash_accept(type)  # 客户端知道 CashSuper 存在
result = csuper.accept_cash(money)            # 客户端需了解 accept_cash() 方法

策略和简单工厂结合

# 客户端代码
from cash_context import CashContext  # 仅需导入 CashContext

type = "满 300 返 100"
money = 350

# 客户端只需与 CashContext 交互
csuper = CashContext(type)           # 完全隐藏 CashSuper 和 CashFactory
result = csuper.get_result(money)    # 统一方法名,隐藏具体实现

在简单工厂中,工厂方法的返回值类型是 CashSuper,客户端必须:

  1. 知道 CashSuper 是抽象父类

  2. 知道调用 accept_cash() 方法(如 csuper.accept_cash(money))

而策略+工厂模式通过 CashContext 封装了工厂和策略的细节,客户端只需调用统一的 get_result()。

小结:

  1. 简单工厂的耦合体现在:客户端需直接操作 CashSuper 的接口

  2. 策略+工厂的改进:通过 CashContext 屏蔽底层细节,使客户端更纯粹


网站公告

今日签到

点亮在社区的每一天
去签到