3

あるタスクを実行する関数があるとします (これは Python 疑似コードです)。

def doTask():
    ...

しかし、プラットフォームにはいくつかのオプション機能があり、その結果、コードは次のようになります。

def doTask():
    ...
    if FEATURE_1_ENABLED:
        ...
    if FEATURE_2_ENABLED:
        ...
    ...

残念ながら、これは互いに一致する多くの異なるオプション機能でかなり厄介になります. この問題を解決する設計パターンは何ですか?

4

2 に答える 2

4

これがコマンド戦略のすべてです。コンポジションと同様に。

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 )

機能を構成し、機能を委任し、機能を継承できます。これは、拡張可能な一連のオプション機能に対して非常にうまく機能します。

于 2009-08-25T02:10:52.627 に答える
1
interface Feature{

  void execute_feature();

}

class Feature1 implements Feature{
  void execute_feature(){}
}
class Feature2 implements Feature{
  void execute_feature(){}
}

public static void main(String argv[]){

List<Feature> my_list = new List<Feature>();
my_list.Add(new Feature1());
my_list.Add(new Feature2());

for (Feature f : my_list){
  f.execute_feature();
}

}

戦略パターンと呼ばれるものだと思います

構文が正確ではない可能性があります

于 2009-08-25T02:10:15.030 に答える