|
| 1 | +# classic_strategy.py |
| 2 | +# Strategy pattern -- classic implementation |
| 3 | + |
| 4 | +""" |
| 5 | + >>> joe = Customer('John Doe', 0) # <1> |
| 6 | + >>> ann = Customer('Ann Smith', 1100) |
| 7 | + >>> cart = [LineItem('banana', 4, .5), # <2> |
| 8 | + ... LineItem('apple', 10, 1.5), |
| 9 | + ... LineItem('watermellon', 5, 5.0)] |
| 10 | + >>> Order(joe, cart, FidelityPromo()) # <3> |
| 11 | + <Order total: 42.00 due: 42.00> |
| 12 | + >>> Order(ann, cart, FidelityPromo()) # <4> |
| 13 | + <Order total: 42.00 due: 39.90> |
| 14 | + >>> banana_cart = [LineItem('banana', 30, .5), # <5> |
| 15 | + ... LineItem('apple', 10, 1.5)] |
| 16 | + >>> Order(joe, banana_cart, BulkItemPromo()) # <6> |
| 17 | + <Order total: 30.00 due: 28.50> |
| 18 | + >>> long_order = [LineItem(str(item_code), 1, 1.0) # <7> |
| 19 | + ... for item_code in range(10)] |
| 20 | + >>> Order(joe, long_order, LargeOrderPromo()) # <8> |
| 21 | + <Order total: 10.00 due: 9.30> |
| 22 | + >>> Order(joe, cart, LargeOrderPromo()) |
| 23 | + <Order total: 42.00 due: 42.00> |
| 24 | +
|
| 25 | +""" |
| 26 | + |
| 27 | +from abc import ABC, abstractmethod |
| 28 | +from collections import namedtuple |
| 29 | + |
| 30 | +Customer = namedtuple('Customer', 'name fidelity') |
| 31 | + |
| 32 | + |
| 33 | +class LineItem: |
| 34 | + |
| 35 | + def __init__(self, product, quantity, price): |
| 36 | + self.product = product |
| 37 | + self.quantity = quantity |
| 38 | + self.price = price |
| 39 | + |
| 40 | + def total(self): |
| 41 | + return self.price * self.quantity |
| 42 | + |
| 43 | + |
| 44 | +class Order: # the Context |
| 45 | + |
| 46 | + def __init__(self, customer, cart, promotion=None): |
| 47 | + self.customer = customer |
| 48 | + self.cart = list(cart) |
| 49 | + self.promotion = promotion |
| 50 | + |
| 51 | + def total(self): |
| 52 | + if not hasattr(self, '__total'): |
| 53 | + self.__total = sum(item.total() for item in self.cart) |
| 54 | + return self.__total |
| 55 | + |
| 56 | + def due(self): |
| 57 | + if self.promotion is None: |
| 58 | + discount = 0 |
| 59 | + else: |
| 60 | + discount = self.promotion.discount(self) |
| 61 | + return self.total() - discount |
| 62 | + |
| 63 | + def __repr__(self): |
| 64 | + fmt = '<Order total: {:.2f} due: {:.2f}>' |
| 65 | + return fmt.format(self.total(), self.due()) |
| 66 | + |
| 67 | + |
| 68 | +class Promotion(ABC): # the Strategy: an Abstract Base Class |
| 69 | + |
| 70 | + @abstractmethod |
| 71 | + def discount(self, order): |
| 72 | + """Return discount as a positive dollar amount""" |
| 73 | + |
| 74 | + |
| 75 | +class FidelityPromo(Promotion): # first Concrete Strategy |
| 76 | + """5% discount for customers with 1000 or more fidelity points""" |
| 77 | + |
| 78 | + def discount(self, order): |
| 79 | + return order.total() * .05 if order.customer.fidelity >= 1000 else 0 |
| 80 | + |
| 81 | + |
| 82 | +class BulkItemPromo(Promotion): # second Concrete Strategy |
| 83 | + """10% discount for each LineItem with 20 or more units""" |
| 84 | + |
| 85 | + def discount(self, order): |
| 86 | + discount = 0 |
| 87 | + for item in order.cart: |
| 88 | + if item.quantity >= 20: |
| 89 | + discount += item.total() * .1 |
| 90 | + return discount |
| 91 | + |
| 92 | + |
| 93 | +class LargeOrderPromo(Promotion): # third Concrete Strategy |
| 94 | + """7% discount for orders with 10 or more distinct items""" |
| 95 | + |
| 96 | + def discount(self, order): |
| 97 | + distinct_items = {item.product for item in order.cart} |
| 98 | + if len(distinct_items) >= 10: |
| 99 | + return order.total() * .07 |
| 100 | + return 0 |
0 commit comments