c# - How do I create Optional Type injection for a base class? -
during course of regular agile programming technique, lot of refactoring. if find common things candidates base class. assume code pattern:
public subclass : baseclass{ private dynamic somevalue = null; public subclass(){ somevalue = baseclass.method<t>(); } }
everything works fine until tired of continually injecting type method. why not inject type base class instead? wait second, don't want have redo 20 classes using pattern above. have 2 potential patterns can use, being second.
public subclass : baseclass<t>{ private t somevalue = null; public subclass(){ somevalue = baseclass.method(); } }
this second pattern logical derivation of first example whereby merely moving type class ctor instead of using generic type in each method.
i ask community thoughts on how accomplish both constructs without changing current code adding in support of generic type baseclass pattern.
i have read other posts on "optional generic class types" of closed stackoverflow nazi soup police.
once again gang of 4 right "favor composition on inheritance". in case maybe term baseclass not appropriate, can see how our baseclass concept wrong @ beginning not asking "is baseclass?"
public subclass { private baseclass bc1 = null; private baseclass<sometype> bc2 = null; private dynamic somevalue = null; public subclass(){ //i still have base class operation ability, now... bc2 = new baseclass<sometype>(); somevalue = bc2.method(); }
}
Comments
Post a Comment