Python-design-patterns-decorator

提供:Dev Guides
移動先:案内検索

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

出力

上記のプログラムは、次の出力を生成します-

デコレーターパターン