Home > Software engineering >  typedef vs extend classes
typedef vs extend classes

Time:07-13

I have a base class

abstract class Repository<T> {
  Future<List<T>> getAll();
  Future<T> getByID(int id);
}

Then I would extend it just to specify type. But Dart has interesting syntax typedef ... = .... What is better?

Classic way

abstract class UserRepository extends Repository<User>{}

Another way

typedef UserRepository = Repository<User>;

Last is more aesthetic

What pros and cons for every way?

CodePudding user response:

You should use inheritance, because typedef required for creating aliases.

abstract class Repository<T> {
  Future<List<T>> getAll();

  Future<T> getByID(int id);
}

// with alias
typedef UserRepository = Repository<User>;

class UserRepositoryImpl extends UserRepository {
    ...
}

// without alias
class UserRepositoryImpl extends Repository<User> {
    ...
}
  • Related