# -*- coding: UTF-8 -*- from discount import DiscountPerItems, DiscountPerValue, NoDiscountAtAll class DiscountCalculator(object): def calculate(self, budget): discounter = DiscountPerItems(DiscountPerValue(NoDiscountAtAll())) return discounter.apply(budget) if __name__ == '__main__': from models import Budget, Item budget = Budget() budget.add(Item('Item A', 100.0)) budget.add(Item('Item B', 50.0)) budget.add(Item('Item C', 400.0)) calculator = DiscountCalculator() discount = calculator.calculate(budget) print('Discount applied: %s' % (discount))
# -*- coding: UTF-8 -*- # State is a behavioral design pattern that lets an # object alter its behavior when its internal state # changes. It appears as if the object changed its class. from models import Budget, Item if __name__ == '__main__': budget = Budget() budget.add(Item('Car', 100.0)) budget.add(Item('Laundry', 50.0)) budget.add(Item('Cake', 400)) print(budget.amount) print(budget.status) budget.approve() # or budget.disapprove() print(budget.status) budget.apply_bonus() print(budget.amount) budget.finish() print(budget.status)