在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ 裝飾器設(shè)計模式
反模式
隊列
適配器設(shè)計模式
享元設(shè)計模式
Python設(shè)計模式
工廠模式
模板設(shè)計模式
構(gòu)建器(Builder)設(shè)計模式
Python設(shè)計模式概要
命令設(shè)計模式
Python設(shè)計模式簡介
觀察者設(shè)計模式
代理設(shè)計模式
異常處理
責(zé)任鏈設(shè)計模式
字典實現(xiàn)
抽象工廠設(shè)計模式
Python并發(fā)(多線程)
策略設(shè)計模式
門面(Facade)設(shè)計模式
原型設(shè)計模式
迭代器設(shè)計模式
集合
單例模式
列表數(shù)據(jù)結(jié)構(gòu)
狀態(tài)設(shè)計模式
模型視圖控制器(MVC)模式
裝飾器設(shè)計模式
面向?qū)ο蟾拍畹膶崿F(xiàn)
面向?qū)ο笤O(shè)計模式
字符串和序列化

裝飾器設(shè)計模式

裝飾器模式允許用戶在不改變其結(jié)構(gòu)的情況下向現(xiàn)有對象添加新功能。 這種類型的設(shè)計模式屬于結(jié)構(gòu)模式,因為此模式充當(dāng)現(xiàn)有類的包裝。

這個模式創(chuàng)建了一個裝飾器類,它封裝了原始類,并提供了額外的功能,保持了類方法簽名的完整性。

裝飾者模式的動機是動態(tài)地附加對象的額外職責(zé)(功能)。

 如何實現(xiàn)裝飾設(shè)計模式?

下面提到的代碼是如何在Python中實現(xiàn)裝飾器設(shè)計模式的簡單演示。 該示例涉及以類形式展示咖啡店(coffeeshop類)。 創(chuàng)建的 coffee 類是一個抽象類,這意味著它不能被實例化。

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'

如下所述,coffeeshop抽象類的實現(xiàn)是通過一個單獨的文件完成的 -

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()))

執(zhí)行上述程序生成以下輸出 -