定义一系列算法,把它们一一封装起来,而且使它们能够相互替
换。本模式使得算法能够独立于使用它的客户而变化。python
假如一个网店制定了下述折扣规则。算法
简单起见,咱们假定一个订单一次只能享用一个折扣。
from abc import ABC, abstractclassmethod from collections import namedtuple Customer = namedtuple('Customer', 'name fidelity') # 相似于购物车 class LineItem(object): def __init__(self, product, quantity, price): self.product = product self.quantity = quantity self.price = price def total(self): return self.price * self.quantity # 上下文是 Order class Order(object): def __init__(self, customer, cart, promotion=None): self.customer = customer self.cart = list(cart) self.promotion = promotion def total(self): ###4 if not hasattr(self, '__total'): self.__total = sum(item.total() for item in self.cart) return self.__total def due(self): ###5 if self.promotion is None: discount = 0 else: discount = self.promotion.discount(self) ###6 return self.total() - discount def __repr__(self): ###2 fmt = '<Order total: {:.2f} due: {:.2f}>' return fmt.format(self.total(), self.due()) ###3 # 策略:抽象基类 class Promotion(ABC): @abstractclassmethod def discount(self, order): """返回折扣金额(正值)""" class FidelityPromo(Promotion): # 第一个具体策略 """为积分为1000或以上的顾客提供5%折扣""" def discount(self, order): ###7 return order.total() * .05 if order.customer.fidelity >= 1000 else 0 class BulkItemPromo(Promotion): # 第二个具体策略 """单个商品为20个或以上时提供10%折扣""" def discount(self, order): discount = 0 for item in order.cart: if item.quantity >= 20: discount += item.total() * .1 return discount class LargeOrderPromo(Promotion): # 第三个具体策略 """订单中的不一样商品达到10个或以上时提供7%折扣""" def discount(self, order): distinct_items = {item.product for item in order.cart} if len(distinct_items) >= 10: return order.total() * .07 return 0 if __name__ == "__main__": # 两位顾客 一个积分是0 一个是1100 longe = Customer('longe', 0) liang = Customer('Ann Smith', 1100) # 购物的商品 cart = [LineItem('banana', 4, .5), LineItem('apple', 10, 1.5), LineItem('watermellon', 5, 5.0)] # 这样去调用 print(Order(longe, cart, FidelityPromo())) ###111 print(Order(liang, cart, FidelityPromo())) banana_cart = [LineItem('banana', 30, .5), LineItem('apple', 10, 1.5)] print(Order(longe, banana_cart, BulkItemPromo())) long_order = [LineItem(str(item_code), 1, 1.0) for item_code in range(10)] print(Order(longe, long_order, LargeOrderPromo()))
from collections import namedtuple Customer = namedtuple('Customer', 'name fidelity') class LineItem: def __init__(self, product, quantity, price): self.product = product self.quantity = quantity self.price = price def total(self): return self.price * self.quantity class Order: # 上下文 def __init__(self, customer, cart, promotion=None): self.customer = customer self.cart = list(cart) self.promotion = promotion def total(self): if not hasattr(self, '__total'): self.__total = sum(item.total() for item in self.cart) return self.__total def due(self): if self.promotion is None: discount = 0 else: discount = self.promotion(self) return self.total() - discount def __repr__(self): fmt = '<Order total: {:.2f} due: {:.2f}>' return fmt.format(self.total(), self.due()) def fidelity_promo(order): """为积分为1000或以上的顾客提供5%折扣""" return order.total() * .05 if order.customer.fidelity >= 1000 else 0 def bulk_item_promo(order): """单个商品为20个或以上时提供10%折扣""" discount = 0 for item in order.cart: if item.quantity >= 20: discount += item.total() * .1 return discount def large_order_promo(order): """订单中的不一样商品达到10个或以上时提供7%折扣""" distinct_items = {item.product for item in order.cart} if len(distinct_items) >= 10: return order.total() * .07 return 0 joe = Customer('John Doe', 0) ann = Customer('Ann Smith', 1100) cart = [LineItem('banana', 4, .5),LineItem('apple', 10, 1.5),LineItem('watermellon', 5, 5.0)] print(Order(joe, cart, fidelity_promo)) print(Order(ann, cart, fidelity_promo)) banana_cart = [LineItem('banana', 30, .5),LineItem('apple', 10, 1.5)] print(Order(joe, banana_cart, bulk_item_promo)) long_order = [LineItem(str(item_code), 1, 1.0) for item_code in range(10)] print(Order(joe, long_order, large_order_promo)) print(Order(joe, cart, large_order_promo))
from collections import namedtuple Customer = namedtuple('Customer', 'name fidelity') class LineItem: def __init__(self, product, quantity, price): self.product = product self.quantity = quantity self.price = price def total(self): return self.price * self.quantity class Order: # 上下文 def __init__(self, customer, cart, promotion=None): self.customer = customer self.cart = list(cart) self.promotion = promotion def total(self): if not hasattr(self, '__total'): self.__total = sum(item.total() for item in self.cart) return self.__total def due(self): if self.promotion is None: discount = 0 else: discount = self.promotion(self) return self.total() - discount def __repr__(self): fmt = '<Order total: {:.2f} due: {:.2f}>' return fmt.format(self.total(), self.due()) def fidelity_promo(order): """为积分为1000或以上的顾客提供5%折扣""" return order.total() * .05 if order.customer.fidelity >= 1000 else 0 def bulk_item_promo(order): """单个商品为20个或以上时提供10%折扣""" discount = 0 for item in order.cart: if item.quantity >= 20: discount += item.total() * .1 return discount def large_order_promo(order): """订单中的不一样商品达到10个或以上时提供7%折扣""" distinct_items = {item.product for item in order.cart} if len(distinct_items) >= 10: return order.total() * .07 return 0 joe = Customer('John Doe', 0) ann = Customer('Ann Smith', 1100) cart = [LineItem('banana', 4, .5), LineItem('apple', 10, 1.5), LineItem('watermellon', 5, 5.0)] banana_cart = [LineItem('banana', 30, .5), LineItem('apple', 10, 1.5)] long_order = [LineItem(str(item_code), 1, 1.0) for item_code in range(10)] ## 找出最大的折扣 promos = [fidelity_promo, bulk_item_promo, large_order_promo] def best_promo(order): """选择可用的最佳折扣""" # print([promo(order) for promo in promos]) return max(promo(order) for promo in promos) print(Order(joe, long_order, best_promo)) print(Order(joe, banana_cart, best_promo)) print(Order(ann, cart, best_promo) )
promos = [globals()[name] for name in globals() if name.endswith('_promo') and name != 'best_promo'] print(promos)
另外一个可行的方法是将全部的策略函数都存放在一个单独的模块中
除了 best_promo,这里咱们将 3 个策略函数存放在了 promotions.py 中设计模式
下面的代码中,最大的变化时内省名为 promotions 的独立模块,构建策略函数列表。
注意,下面要导入 promotions 模块,以及高阶内省函数的 inspect 模块app
import inspect import promotions promos = [func for name, func in inspect.getmembers(promotions, inspect.isfunction)] def best_promo(order): """选择可用的最佳折扣 """ return max(promo(order) for promo in promos) print(Order(joe, long_order, best_promo)) print(Order(joe, banana_cart, best_promo)) print(Order(ann, cart, best_promo) )
class MacroCommand: """一个执行一组命令的命令""" def __init__(self, commands): self.commands = list(commands) def __call__(self): for command in self.commands: command()
1.python 对某些设计默认 能够用纯函数来实现, 不用能够去写类
2.设计模式得看看了ide