每次看到项目中存在大量的if else代码时,都会心生一丝不安全感。 特别是产品给的需求需要添加或者更改一种if条件时,生怕会因为自己的疏忽而使代码天崩地裂,哈哈,本文的目的就是来解决这种不安全感的,23种设计模式的策略模式。
GOF对策略模式的解释是: 定义一系列算法, 把它们一个个封装起来,并且使它们可相互替换(变化)。该模式使得算法可独立于使用它的客户程序(稳定)而变化(扩展, 子类化)。
策略模式结构图如下:
生活中商场搞活动给消费者派送了一批优惠券,优惠券分三种, 一种是满减券满100减10元, 一种是打折券满200打8折, 一种折扣券是满300打7.5折然后送鸡蛋一打(价值15),如果我们是商场后台程序员,我们要计算每个客户最后实际支付了多少钱,那这个程序你会怎样去设计?
class Order: def __init__(self, money, coupon): self.money = money self.coupon = coupon def total_money(self): if self.coupon == 1 and self.money >= 100: return self.money - 10 elif self.coupon == 2 and self.money >= 200: return self.money * 0.8 elif self.coupon == 3 and self.money >= 300: return self.money * 0.75 -15 return self.money if __name__ == ‘__main__‘: order1 = Order(102, 1) order2 = Order(250, 2) order3 = Order(372, 3) order4 = Order(190, 2) o1 = order1.total_money() o2 = order2.total_money() o3 = order3.total_money() o4 = order4.total_money() print(o1, o2, o3, o4)
在平常的开发过程中,我们及其容易设计出这样的代码,夹杂着大量的if else语句,程序耦合性很高,一旦发生修改不小心就天崩地裂了, 严重违反我们八大设计原则的开闭原则。
接下来我们用策略设计模式来设计我们的程序。
from abc import ABC, abstractmethod class BaseStrategy(ABC): @abstractmethod def calculate(cls, money): pass
策略基类继承ABC类(抽象类)在方法上加上abstractmethod表示子类必须重写这个方法,不然会报错。
class FullOneHundredStrategy(BaseStrategy): @classmethod def calculate(cls, money): if money >= 100: return money - 10 return money class FullTwoHundredStrategy(BaseStrategy): @classmethod def calculate(cls, money): if money >= 200: return money * 0.8 return money class FullThreeHundredStrategy(BaseStrategy): @classmethod def calculate(cls, money): if money >= 300: return money * 0.75 - 15 return money
分别设计三个策略子类并重写对应的计算方法,如果以后新增新的优惠券就可以增加一个新的子类即可。
class Order: coupon_map = { 1: FullOneHundredStrategy, 2: FullTwoHundredStrategy, 3: FullThreeHundredStrategy } def __init__(self, money, coupon): self.money = money self.coupon = coupon def total_money(self): return self.coupon_map.get(self.coupon).calculate(self.money)
订单类包括实例属性money,和优惠券的种类, 并有一个类属性map,在实际开发中这个Map一般会放到公共文件中。
if __name__ == ‘__main__‘: order1 = Order(102, 1) order2 = Order(250, 2) order3 = Order(372, 3) order4 = Order(190, 2) o1 = order1.total_money() o2 = order2.total_money() o3 = order3.total_money() o4 = order4.total_money() print(o1, o2, o3, o4)
我们打印结果:
和我们期望的结果一样,这样就完美的消除了大量的if else语句了, 更重要的是这样使策略与订单类解耦了,增加了程序的可复用性,和维护性。
策略设计模式的好处:
策略模式的劣处:
ps:前后判断强关联如上述举例改成:商场活动不需要优惠券,只要你消费满100就减10,满200打8折,这样前后判断就有比较强的关联性了,此时的策略模式就不能有效消除if else判断语句了。
在最后我们依然需要加上我们设计模式的八大原则:
原文:https://www.cnblogs.com/lifei01/p/13208824.html