
- Python 設計模式教程
- Python 設計模式 - 首頁
- 簡介
- Python 設計模式 - 要點
- 模型-檢視-控制器模式
- Python 設計模式 - 單例模式
- Python 設計模式 - 工廠模式
- Python 設計模式 - 建造者模式
- Python 設計模式 - 原型模式
- Python 設計模式 - 外觀模式
- Python 設計模式 - 命令模式
- Python 設計模式 - 介面卡模式
- Python 設計模式 - 裝飾器
- Python 設計模式 - 代理模式
- 責任鏈模式
- Python 設計模式 - 觀察者模式
- Python 設計模式 - 狀態模式
- Python 設計模式 - 策略模式
- Python 設計模式 - 模板模式
- Python 設計模式 - 享元模式
- 抽象工廠模式
- 面向物件
- 面向物件概念實現
- Python 設計模式 - 迭代器模式
- 字典
- 列表資料結構
- Python 設計模式 - 集合
- Python 設計模式 - 佇列
- 字串 & 序列化
- Python 中的併發
- Python 設計模式 - 反模式
- 異常處理
- Python 設計模式資源
- 快速指南
- Python 設計模式 - 資源
- 討論
Python 設計模式 - 裝飾器
裝飾器模式允許使用者向現有物件新增新功能,而無需更改其結構。這種設計模式屬於結構型模式,因為它充當現有類的包裝器。
此模式建立一個裝飾器類,該類包裝原始類並提供額外的功能,同時保持類方法簽名不變。
裝飾器模式的目的是動態地附加物件的額外職責。
如何實現裝飾器設計模式
下面提到的程式碼簡單演示瞭如何在 Python 中實現裝飾器設計模式。該示例以類的形式演示了咖啡店的場景。建立的咖啡類是抽象的,這意味著它不能被例項化。
import six from abc import ABCMeta @six.add_metaclass(ABCMeta) class Abstract_Coffee(object): def get_cost(self): pass def get_ingredients(self): pass def get_tax(self): return 0.1*self.get_cost() class Concrete_Coffee(Abstract_Coffee): def get_cost(self): return 1.00 def get_ingredients(self): return 'coffee' @six.add_metaclass(ABCMeta) class Abstract_Coffee_Decorator(Abstract_Coffee): def __init__(self,decorated_coffee): self.decorated_coffee = decorated_coffee def get_cost(self): return self.decorated_coffee.get_cost() def get_ingredients(self): return self.decorated_coffee.get_ingredients() class Sugar(Abstract_Coffee_Decorator): def __init__(self,decorated_coffee): Abstract_Coffee_Decorator.__init__(self,decorated_coffee) def get_cost(self): return self.decorated_coffee.get_cost() def get_ingredients(self): return self.decorated_coffee.get_ingredients() + ', sugar' class Milk(Abstract_Coffee_Decorator): def __init__(self,decorated_coffee): Abstract_Coffee_Decorator.__init__(self,decorated_coffee) def get_cost(self): return self.decorated_coffee.get_cost() + 0.25 def get_ingredients(self): return self.decorated_coffee.get_ingredients() + ', milk' class Vanilla(Abstract_Coffee_Decorator): def __init__(self,decorated_coffee): Abstract_Coffee_Decorator.__init__(self,decorated_coffee) def get_cost(self): return self.decorated_coffee.get_cost() + 0.75 def get_ingredients(self): return self.decorated_coffee.get_ingredients() + ', vanilla'
咖啡店的抽象類的實現是在一個單獨的檔案中完成的,如下所示:
import coffeeshop myCoffee = coffeeshop.Concrete_Coffee() print('Ingredients: '+myCoffee.get_ingredients()+ '; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax())) myCoffee = coffeeshop.Milk(myCoffee) print('Ingredients: '+myCoffee.get_ingredients()+ '; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax())) myCoffee = coffeeshop.Vanilla(myCoffee) print('Ingredients: '+myCoffee.get_ingredients()+ '; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax())) myCoffee = coffeeshop.Sugar(myCoffee) print('Ingredients: '+myCoffee.get_ingredients()+ '; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax()))
輸出
以上程式生成以下輸出:

廣告