Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1bba1c

Browse files
committed
设计模式
1 parent b7c6e70 commit a1bba1c

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

‎06-dp-1class-func/promotions.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
def fidelity_promo(order):
4+
"""5% discount for customers with 1000 or more fidelity points"""
5+
return order.total() * 0.05 if order.customer.fidelity >= 1000 else 0
6+
7+
8+
def bulk_item_promo(order):
9+
"""10% discount for each LineItem with 20 or more units"""
10+
discount = 0
11+
for item in order.cart:
12+
if item.quantity >= 20:
13+
discount += item.total() * 0.1
14+
return discount
15+
16+
17+
def large_order_promo(order):
18+
"""7% discount for orders with 10 or more distinct items"""
19+
distinct_items = {item.product for item in order.cart}
20+
if len(distinct_items) >= 10:
21+
return order.total() * 0.07
22+
return 0

‎06-dp-1class-func/strategy.py‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from collections import namedtuple
4+
import inspect
5+
6+
import promotions
7+
8+
Customer = namedtuple('Customer', 'name fidelity')
9+
10+
11+
class LineItem:
12+
13+
def __init__(self, product, quantity, price):
14+
self.product = product
15+
self.quantity = quantity
16+
self.price = price
17+
18+
def total(self):
19+
return self.price * self.quantity
20+
21+
22+
class Order:
23+
24+
def __init__(self, customer, cart, promotion=None):
25+
self.customer = customer
26+
self.cart = list(cart)
27+
self.promotion = promotion
28+
29+
def total(self):
30+
if not hasattr(self, '__total'):
31+
self.__total = sum(item.total() for item in self.cart)
32+
return self.__total
33+
34+
def due(self):
35+
if self.promotion is None:
36+
discount = 0
37+
else:
38+
discount = self.promotion(self)
39+
return self.total() - discount
40+
41+
def __repr__(self):
42+
fmt = '<Order total: {:.2f} due: {:.2f}>'
43+
return fmt.format(self.total(), self.due())
44+
45+
46+
def fidelity_promo(order):
47+
"""5% discount for customers with 1000 or more fidelity points"""
48+
return order.total() * 0.05 if order.customer.fidelity >= 1000 else 0
49+
50+
51+
def bulk_item_promo(order):
52+
"""10% discount for each LineItem with 20 or more units"""
53+
discount = 0
54+
for item in order.cart:
55+
if item.quantity >= 20:
56+
discount += item.total() * 0.1
57+
return discount
58+
59+
60+
def large_order_promo(order):
61+
"""7% discount for orders with 10 or more distinct items"""
62+
distinct_items = {item.product for item in order.cart}
63+
if len(distinct_items) >= 10:
64+
return order.total() * 0.07
65+
return 0
66+
67+
68+
# best promotion version 1
69+
promos_v1 = [fidelity_promo, bulk_item_promo, large_order_promo]
70+
71+
72+
def best_promo_v1(order):
73+
"""Select best discount available"""
74+
return max(promo(order) for promo in promos_v1)
75+
76+
77+
# best promotion version 2
78+
promos_v2 = [globals()[name] for name in globals() if name.endswith('_promo')]
79+
80+
81+
def best_promo_v2(order):
82+
"""Select best discount available"""
83+
return max(promo(order) for promo in promos_v2)
84+
85+
86+
# best promotion version 3
87+
promos_v3 = [func for name, func in
88+
inspect.getmembers(promotions, inspect.isfunction)]
89+
90+
91+
def best_promo_v3(order):
92+
"""Select best discount available"""
93+
return max(promo(order) for promo in promos_v3)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /