.net - Best design pattern for calculated values on a subclass? -
say have 2 types:
classa { int valuea; int calculateda; } classb { int valuea; int calculateda; int valueb; int calculatedb; }
calculatedb
requires valuea
valueb
. i'm trying decide best pattern implement this.
option 1: subclass classa
, add values. have common update()
method overridden in subclass. simple code in model, code create these classes needs know create in advance, , code iterates on list of these types needs type checking deal fields.
option 2: have properties in separate class, , have update code calculatedb
there. issue classb
needs way of knowing when valuea
updated, , hoping not have implement inotifypropertychanged
on these classes. way have sort of public update method on properties class, , have main class call method when valuea
updated. not desirable.
option 3: have classb
valueb
, calculatedb
being nullable types. pass.
are there more? choose?
if classb not related classa, subclassing not method. btw, in .net don't expose public fields, public properties (assume c#)
public class classa{ public int valuea {get;set;} //... }
since classb.calculateb heavily rely on valuea, why not calculate value on fly, don't have worry property changing.
public class classb { public int valueb {get;set;} public int getcalculateb(classa a){ //... } }
Comments
Post a Comment