Sobes.tech
Junior — Middle

Can you explain the meaning of the letter D in the SOLID principles?

sobes.tech AI

Answer from AI

The letter D in SOLID principles stands for Dependency Inversion Principle.

The essence of this principle:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.

This helps reduce code coupling and increase flexibility and testability.

Example in Dart/Flutter:

abstract class Database {
  void saveData(String data);
}

class SqlDatabase implements Database {
  @override
  void saveData(String data) {
    print('Saving data to SQL DB: $data');
  }
}

class DataManager {
  final Database database;

  DataManager(this.database);

  void save(String data) {
    database.saveData(data);
  }
}

void main() {
  Database db = SqlDatabase();
  DataManager manager = DataManager(db);
  manager.save('Example data');
}

Here, DataManager depends not on a specific database implementation but on the abstraction Database. This allows easy swapping of database implementations without changing DataManager.