これがコマンドと戦略のすべてです。コンポジションと同様に。
class Command( object ):
def do( self ):
raise NotImplemented
class CompositeCommand( Command, list ):
def do( self ):
for subcommand in self:
subcommand.do()
class Feature_1( Command ):
def do( self, aFoo ):
# some optional feature.
class Feature_2( Command ):
def do( self, aFoo ):
# another optional feature.
class WholeEnchilada( CompositeCommand ):
def __init__( self ):
self.append( Feature_1() )
self.append( Feature_2() )
class Foo( object ):
def __init__( self, feature=None ):
self.feature_command= feature
def bar( self ):
# the good stuff
if self.feature:
self.feature.do( self )
機能を構成し、機能を委任し、機能を継承できます。これは、拡張可能な一連のオプション機能に対して非常にうまく機能します。