Replacement pattern for an anonymous class in Dart
I don't think there's a general answer for your question. It really depends on your code. You can ever create a class for all anonymous class or refactor your API and try to avoid them.
For instance your sample code could be refactored to use functions to 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 what the underlying code could be :
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 }}