Replacement pattern for an anonymous class in Dart -
i'm trying build base class table< t > uses list tablecolumns< t > display table in dart. in java done anonymous class pattern leaf class simple following:
class parttable extends table< part > { // column 1: part number addcolumn( new tablecolumn<part>("part number") { string get(part part) { part.getnumber().tostring() } void set(part part, string number) { part.setnumber(number); } }); // column 2: part description addcolumn( new tablecolumn<part>("part description") { string get(part part) { part.getdescription().tostring() } void set(part part, string desc) { part.setdescription(desc); } }); }
can suggest dart design pattern nicely substitute java anonymous class pattern above? basic idea anonymous tablecolumn class abstract methods (e.g. , set) overridden by anonymous class in table constructor provide view specific overrides working part's attributes. table can have arbitrary number of columns, , provide view specific crud behavior each parts attributes in tables cell.
i don't think there's general answer question. depends on code. can ever create class anonymous class or refactor api , try avoid them.
for instance sample code refactored use functions define get
/set
:
final table = new table<part>() ..addcolumn(new column<part>( "part number", (p) => p.number.tostring(), (p, s) => p.number = s)) ..addcolumn(new column<part>( "part description", (p) => p.desc.tostring(), (p, s) => p.desc = s));
here's underlying code :
typedef string columngetter<t>(t t); typedef void columnsetter<t>(t t, string s); class column<t> { final string name; final columngetter<t> get; final columnsetter<t> set; column(this.name, this.get, this.set); } class table<t> { list<t> _elements; final _columns = []; void addcolumn(column<t> column) => _columns.add(column); void set elements(list<t> elements) { this._elements = elements; // updatedisplay } }
Comments
Post a Comment