Home > Software engineering >  why do we use abstract classes in clean architecture?
why do we use abstract classes in clean architecture?

Time:10-12

we have abstract classes in clean architecture where we just define functions and then write the whole code in an implementation class that implements those functions. why not use only the second function?

abstraction

abstract class PriceTrackerDataSource {
  Stream<dynamic> getActiveSymbols();
}

implementation

class PriceTrackerDataSourceImpl implements PriceTrackerDataSource {
  @override
  Stream<dynamic> getActiveSymbols() async* {
    if (_ws != null) {
      await _ws!.sink.close();
    }

    _connect();
    yield* _ws!.stream;
  }

CodePudding user response:

Abstract classes are used to make each layers more independent (data, domain, presentation). And also to define main methods for the own classes

CodePudding user response:

Abstract classes help us to define a template / common definition for our multiple derived classes also reducing code duplication.

Making the class abstract ensures that it cannot be used on its own, but the details must be defined in the derived class implementation.

For more explanation you can have a look at Dart Docs.

  • Related