Junior — Middle
Kas saaksite selgitada tähe D tähendust SOLID põhimõtetes?
sobes.tech AI
Vastus AI-lt
The letter D in the 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 to reduce coupling in the code and increase its 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 does not depend on a concrete database implementation, but on the abstraction Database. This allows easy swapping of the database implementation without modifying DataManager.