知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
class Cart: def __init__(self): self.products = [] def add(self, obj): self.products.append(obj) def notify(self): for obj in self.products: obj.notify(len(self.products)) def price(self): for obj in self.products: print(obj.price) class Checkout(): def __init__(self,cart): self.cart = cart self.products = self.cart.products self.totals = 0 self.accounting() def total(self): print(self.totals) return (self.totals) def accounting(self): for obj in self.products: self.totals = self.totals + obj.price def bill(self): pass class Shipping(): def __init__(self,checkout,ship): self.ship = ship checkout.totals = checkout.totals + ship.getCost() def cost(self): print(self.ship.getCost()) class UPS(): def __init__(self): self.cost = 15.2 def getCost(self): return self.cost class FedEx(Shipping): def __init__(self): self.cost = 10 def getCost(self): return self.cost class Payment(): def __init__(self, checkout): pass def payable(self): pass def tax(self): pass
+-------------+ +-----------------------+ User -> Goods -> Cart -> | Delivery | -> | Promotions components | -> Checkout +-------------+ +-----------------------+ | rule A | | Promotion rule 1 | | rule B | | Promotion rule 2 | | rule C | | Promotion rule 3 | | rule D | | Promotion rule 4 | | rule E | | Promotion rule 5 | +-------------+ +-----------------------+
資料庫設計
+--------------+ | shipping | +--------------+ | id |o---+ | name | | | ... | | +--------------+ | +----------------------+ | | shipping_rule | +--------------+ | +----------------------+ | zone | | | id | +--------------+ +--->| shipping_id | | id |o------->| zone_id | | name | | plugin | | | | ... | +--------------+ +----------------------+ +--------------+ | delivery | +--------------+ | id | | user_id |o------> | address_id |o------> +--------------+