-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstrategy_pattern_object_oriented.py
91 lines (73 loc) · 2.53 KB
/
strategy_pattern_object_oriented.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
>>> joe = Customer('John Doe', 0)
>>> ann = Customer('Ann Smith', 1100)
>>> cart = [LineItem('banana', 4, 0.5),
... LineItem('apple', 20, 1.5),
... LineItem('watermello', 5, 5.0)]
>>> Order(joe, cart, FidelityPromo())
<Order total: 57.00 due: 57.00>
>>> Order(ann, cart, FidelityPromo())
<Order total: 57.00 due: 54.15>
>>> Order(joe, cart, BulkItemPromo())
<Order total: 57.00 due: 54.00>
"""
from abc import abstractmethod, ABC
from collections import namedtuple
# Customer class
Customer = namedtuple('Customer', 'name fidelity')
class LineItem:
def __init__(self, product, quantity, price):
self.product = product
self.quantity = quantity
self.price = price # unit price
def total(self):
return self.quantity * self.price
class Order: # the context
def __init__(self, customer, cart, promotion=None):
self.customer = customer
self.cart = list(cart) # copy it
self.promotion = promotion
def total(self):
if not hasattr(self, '__total'): # by calling getattr and catch
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.discount(self)
return self.total() - discount
def __repr__(self):
fmt = '<Order total: {:.2f} due: {:.2f}>'
return fmt.format(self.total(), self.due())
# The strategy, an Abstract Base Class
class Promotion(ABC):
@abstractmethod
def discount(self, order):
"""Return discount as positive dollar amount"""
# Series of Concrete Strategy
class FidelityPromo(Promotion):
"""5% discount for customers with 1000 or more fidelity points"""
def discount(self, order):
if order.customer.fidelity >= 1000:
return order.total() * 0.05
else:
return 0
class BulkItemPromo(Promotion):
"""10% discount for each LineItem with 20 or more units"""
def discount(self, order):
discount_ = 0
for item in order.cart:
if item.quantity >= 20:
discount_ += item.total() * 0.1
return discount_
class LargeOrderPromo(Promotion):
"""7% discount for orders with 10 or more distinct items"""
def discount(self, order):
distinct_items = {item.product for item in order.cart}
if(len(distinct_items)) >= 10:
return order.total() * 0.07
return 0
if __name__ == "__main__":
import doctest
doctest.testmod()