I was wondering which is the best way to extend the CustomEvent
class, a class which has only one factory constructor. I tried doing the following and ran into an issue with the super constructor :
class MyExtendedEvent extends CustomEvent {
int count;
factory MyExtendedEvent(num count) {
return new MyExtendedEvent._internal(1);
}
MyExtendedEvent._internal(num count) {
this.count = count;
}
}
but I can't get it working. I always run into :
unresolved implicit call to super constructor 'CustomEvent()'
If i try chaning the internal constructor to :
MyExtendedEvent._internal(num count) : super('MyCustomEvent') {
this.count = count;
}
I end up with :
'resolved implicit call to super constructor 'CustomEvent()''.
I'm not sure what I'm doing wrong - but I guess the problem is that the CustomEvent
has only one constructor which is a factory constructor (as doc says - http://api.dartlang.org/docs/releases/latest/dart_html/CustomEvent.html)
What is the best way to extend a CustomEvent
, or any class of this form?
The factory method is a smart way to create objects in Java and provides several advantages over the traditional approach of creating objects using constructors in Java. It can also improve the quality of code by making the code more readable, less coupled, and improves performance by caching.
A factory constructor is a constructor that can be used when you don't necessarily want a constructor to create a new instance of your class. This might be useful if you hold instances of your class in memory and don't want to create a new one each time (or if the operation of creating an instance is costly).
Factory constructors return am instance of the class, but it doesn't necessarily create a new instance. Factory constructors might return an instance that already exists, or a sub-class.
You can inherit from or extend a class using the extends keyword. This allows you share properties and methods between classes that are similar, but not exactly the same. Also, it allows different subtypes to share a common runtime type so that static analysis doesn't fail.
You can't directly extend a class with a factory constructor. However you can implement the class and use delegation to simulate the extension.
For instance :
class MyExtendedEvent implements CustomEvent {
int count;
final CustomEvent _delegate;
MyExtendedEvent(this.count, String type) :
_delegate = new CustomEvent(type);
noSuchMethod(Invocation invocation) =>
reflect(_delegate).delegate(invocation);
}
NB : I used reflection here to simplify the code snippet. A better implementation (in regard of performances) would have been to define all methods like method1 => _delegate.method1()
Unfortunately, you can't extend a class if it only has factory constructors, you can only implement it. That won't work well with CustomEvent though since it's a DOM type, which is also why it only has factory constructors: the browser has to produce these instances, the Dart object is just a wrapper. If you try to implement CustomElement and fire one, you'll probably get an error.
You can now use the extension
feature of Dart (from Dart 2.7) to get a similar behaviour if you just want to add some methods to the existing class.
extension MyExtensionEvent on CustomEvent {
void increaseCount() => count++;
}
and then when you want to use your newly created extension you just import it and then call an instance of the original class but with one of your newly created methods:
import event_extension.dart
final event = CustomEvent(1);
event.increaseCount();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With