Since I have two similar classes, but with little differences in only one function, I want to create a base class that they will extend, but that will also force them to implement that specific function. To better explain this:
class A {
void print() {print "hello";}
void func (){}
}
class B extends A {
#override func(){
//TODO
}
}
you can use an abstract base class, that is the parent of both classes
abstract class Base {
void func();
}
class A extends Base{
#override
void func() {
}
}
class B extends Base {
#override
void func() {
}
}
Related
I want to reduce the verbosity of binding a generic interface to several implementations based on TypeLiterals...
I have an interface FieldComputer<T extends ComputeField> where ComputeField is my model interface.
Tried extending a ShortLiteral class (see example below) to reduce the verbosity but it doesn't seem to work. would like to understand why?
// A typical Guice Module
public class ConflationModule implements Module {
// typical overridden configure method
public void configure(Binder binder) {
// Works but is verbose....
bindField_1(binder,
new TypeLiteral<FieldComputer<ComputeFieldImpl>>(){},
FieldComputerImpl.class);
// Doesn't Work
bindField_1(binder,
new ShortLiteral<ComputeFieldImpl>(){},
FieldComputerImpl.class);
// Doesn't Work
bindField_2(binder,
new ShortLiteral<ComputeFieldImpl>(){},
FieldComputerImpl.class);
}
private static class ShortLiteral<CF extends ComputeField> extends TypeLiteral<FieldComputer<CF>>{}
private <CF extends ComputeField> void bindField_1(Binder binder,
TypeLiteral<FieldComputer<CF>> typeLiteral,
Class<? extends FieldComputer<CF>> clazz
) {
binder.bind(typeLiteral).to(clazz);
}
private <CF extends ComputeField> void bindField_2(Binder binder,
ShortLiteral<CF> typeLiteral,
Class<? extends FieldComputer<CF>> clazz
) {
binder.bind(typeLiteral).to(clazz);
}
}
I would suggest you just create TypeLiteral programmatically, here is an example how to do it with different implementations of one interface:
class TypeLiteralModule extends AbstractModule {
#Override
protected void configure() {
customBind(String.class, StringConsumer.class);
customBind(Integer.class, IntegerConsumer.class);
}
private <T> void customBind(Class<T> clazz, Class<? extends Consumer<T>> impl) {
var typeLiteral = (TypeLiteral<Consumer<T>>) TypeLiteral.get(Types.newParameterizedType(Consumer.class, clazz));
bind(impl).in(Singleton.class);
bind(typeLiteral).to(impl);
}
}
class StringConsumer implements Consumer<String> {
#Override
public void accept(String s) {
}
}
class IntegerConsumer implements Consumer<Integer> {
#Override
public void accept(Integer s) {
}
}
Lets say that I have an abstract class
abstract class OnClickHandler {
void doA();
void doB();
}
I have a class
class MyClass {
OnClickHandler onClickHandler;
MyClass({
this.onClickHandler
})
void someFunction() {
onClickHandler.doA();
}
}
And I have a class
class Main implements onClickHandler {
// This throws me an error
MyClass _myClass = MyClass(onClickHandler = this); // <- Invalid reference to 'this' expression
#override
void doA() {}
#override
void doB() {}
}
How can I say that use the same implementations that the Main class has? or is there an easier/better way to do this?
Your problem is that this does not yet exists since the object are still being created. The construction of Dart objects is done in two phases which can be difficult to understand.
If you change you program to the following it will work:
abstract class OnClickHandler {
void doA();
void doB();
}
class MyClass {
OnClickHandler onClickHandler;
MyClass({this.onClickHandler});
void someFunction() {
onClickHandler.doA();
}
}
class Main implements OnClickHandler {
MyClass _myClass;
Main() {
_myClass = MyClass(onClickHandler: this);
}
#override
void doA() {}
#override
void doB() {}
}
The reason is that code running inside { } in the constructor are executed after the object itself has been created but before the object has been returned from the constructor.
My background is from java, so i can implement abstract classes and methods in java like given bellow:
Class 1
public class Base {
public void method( VerificationCallbacks verificationCallbacks){
verificationCallbacks.signInWithEmail();
};
}
Abstract class
public abstract class VerificationCallbacks {
public abstract void signInWithEmail();
public abstract void signUpInWithEmail();
}
so we can implement these classes like
Base base = new Base();
base.method(new VerificationCallbacks() {
#Override
public void signInWithEmail() {
}
#Override
public void signUpInWithEmail() {
}
});
But now i want to implement this technique in dart or flutter
Base base = new Base();
base.method(new VerificationCallbacks());
but when i write this code to implement override methods, it shows abstract classes cannot be instantiated dart, please anyone can help me to achieve this.
class Base {
void method({
VoidCallback signInWithEmailCallback,
VoidCallback signUpWithEmailCallback,
}) {
if (true) {
signInWithEmailCallback();
} else {
signUpWithEmailCallback();
}
}
}
and
Base base = Base();
base.method(signInWithEmailCallback: () {
//
}, signUpWithEmailCallback: () {
//
});
also you can define you own alias for callback like this
typedef VerificationCallback = void Function();
and use it
class Base {
void method({
VerificationCallback signInWithEmailCallback,
VerificationCallback signUpWithEmailCallback,
}) {
// logic here
}
}
Being new to Dart, I am not sure how I could extend the class of my statefull widget. This is what one of my class looks like
class HomeState extends State<Home>
{
}
What I would like to do is to make the HomeState class inherit some methods from my custom class. Which would be something like this
class CustomClass
{
void DisplayBusyDialog()
{
......
}
}
I know in Dart like Java you cannot inherit from multiple class (unlike C++).
My question is what would I need to do to have HomeState inherit from CustomClass ? Will my custom class need to inherit from StatefulWidget and then have HomeState class extend CustomClass ? what about the template State how do I handle that in my custom Class? Any suggestions would be appreciated ..
What you'll want is mixin, which allows multiple "extends".
class Bar {
bar() {}
}
mixin Mixin {
foo() {}
}
class Foo extends Bar with Mixin {
hello() {
foo();
bar();
}
}
And if you need your mixin the access fields from the base-class:
mixin Mixin on Bar {
foo() {
bar();
}
}
According to Dart programming language, the mixins has the solution to extend multiple parent classes. Anyhow if you want to follow the traditional object-oriented concept you can create an abstract class with a generic type.
abstract class _BaseStatefulState<T extends StatefulWidget> extends State<T> {
_BaseStatefulState() {
// Parent constructor
}
void baseMethod() {
// Parent method
}
}
Your StatefulWidget should be extended from the base like below:
class _MainScreenState extends _BaseStatefulState<MainScreen> {}
Example StatefulWidget:
import 'package:flutter/material.dart';
class MainScreen extends StatefulWidget {
#override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends _BaseStatefulState<MainScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text("Test")),
);
}
}
abstract class _BaseStatefulState<T extends StatefulWidget> extends State<T> {
_BaseStatefulState() {
// Parent constructor
}
void baseMethod() {
// Parent method
}
}
Source reference:
https://gist.github.com/aslamanver/e1360071f9caff009101eb190a38d4cb
Flutter mixin documentation:
https://dart.dev/guides/language/language-tour#adding-features-to-a-class-mixins
The parser is complaining that the property inheritor .list is not subtype of ModelList.list, but LeadsGroup does inherit from Model.
Is this wrong? What is the correct way to do this?
My base class:
abstract class ModelList {
List<Model> get list;
set list(List<Model> n);
}
The inheritor:
class ListLeadsGroup extends ModelList {
List<LeadsGroup> list;
}
class LeadsGroup extends Model {
}
If you have code like
class Foo extends ModelList {}
ModelList ml = new ListLeadsGroup();
ml.list.add(new Foo());
ml.list is of type Model, therefore adding Foo should be legit.
But this very likely is not what you want.
This is why List<ListLeadsGroup> can't override List<Model>.
This should do what you want:
abstract class ModelList<T extends Model> {
List<T> get list;
set list(List<T> n);
}
class ListLeadsGroup extends ModelList<LeadsGroup> {
List<LeadsGroup> list;
}
class LeadsGroup extends Model {
}
just copied from Matan Lurey's comment on Gitter
import 'package:func/func.dart';
class ModelRegistry {
final _factories = <Type, Func0<Model>>{};
Model create(Type type) => _factories[type]();
void register(Type type, Model factory()) {
_factories[type] = factory;
}
}
main() {
var registry = new ModelRegistry();
registry.register(FooModel, () => new FooModel());
var foo = registry.create(FooModel);
}
found a solution using the new keyword covariant. Now the classes that extends ModelList can override the List<Model> list without warnings.
#serializable
abstract class ModelList extends ModifiedModel
implements Model {
Type get listType {
throw new UnimplementedError();
}
List<Model> get list;
set list(covariant List n);
}