'''Classic policy mode: encapsulates a series of algorithms that can be replaced by each other, so that the algorithms can change independently and the customers who use them. Assume that a clothing store in contemporary mall has three discount rules: 1. for Members, 8.5 off for all products. 2. Buy two or more items for the same product, except for the first item, the remaining 7.5 off 3, buy 5 different products, all goods off three rules can only enjoy one. '''From abc import ABC, abstractmethodfrom collections import namedtupleCustomer = namedtuple ('customer', 'name vip ') # consumer object class Clothing: # clothing def _ init _ (self, name, quantity, price): self. name = name self. quantity = quantity self. price = price def total (self): return self. price * self. quantityclass Order: # Order class def _ init _ (self, customer, clothing, promotion = None): self. customer = customer self. clothing = list (clothing) self. promotion = promotion def total (self): if not hasattr (self, '_ total'): self. _ total = sum (item. total () for item in self. clothing) 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 () class Promotion (ABC): # create three discount base classes @ abstractmethod def discount (self, order ):''': returns calculates the discount ''' class VipPromo (Promotion): # member discount ": returns member 8.5 off" def discount (self, order): return order. total ()*. 15 if order. customer. vip else 0 class TwoPromo (Promotion): # Second ''': returns more than two pieces of clothing, second and later ''' def discount (self, order): discount = 0 for item in order. clothing: if item. quantity> = 2: discount + = (item. total ()-item. price )*. 25 return discountclass FivePromo (Promotion): # more than 5 "": returns buy more than 5 different kinds of clothing, off per piece "" def discount (self, order ): distinct_items = {item. name for item in order. clothing} if len (distinct_items)> = 5: return order. total ()*. 2 return 0if _ name _ = "_ main _": # create a consumer, joe = Customer ('John Doe ', 0) # non-member ann = Customer ('ann Smith ', 1) # member # create a shopping cart clothing = [Clothing ('pants', 6,200), Clothing ('skirt', 1,150 ), clothing ('shoes', 2,230)] print (Order (joe, clothing, VipPromo () # <Order total: 1810.00 due: 1810.00> not a member, print (Order (ann, clothing, VipPromo () # <Order total: 1810.00 due: 1538.50> Members, print (Order (joe, clothing, twoPromo () # <Order total: 1810.00 due: 1502.50> print (Order (joe, clothing, FivePromo () for more than two pieces (Order () # <Order total: 7.5 due: 1810.00> No discounts for no more than 5 types of clothing. extend ([Clothing ('shirt', 1, 90), Clothing ('br', 2,130)]) print (Order (joe, clothing, FivePromo ())) # <Order total: 2160.00 due: 1728.00> off for 5 types |