Is there a good design pattern for implementing additional functionality?
Suppose I have a function that does some task (this is in Python pseudocode):
def doTask():
...
But I have a few additional features on the platform that result in code that looks like this:
def doTask():
...
if FEATURE_1_ENABLED:
...
if FEATURE_2_ENABLED:
...
...
Unfortunately, it gets rather messy with many different additional features that overlap with each other. What design patterns solve this problem?
+2
source to share
2 answers
That's what Team and Strategy are . Just like Composition .
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 )
You can create functions, delegate functions, inherit functions. This works very well for an expandable set of advanced features.
+4
source to share
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();
}
}
I think this is called the strategic pattern
Syntax may not be accurate
+1
source to share