Middle+
What is SOLID?
sobes.tech AI
Answer from AI
SOLID is a set of five principles of object-oriented programming aimed at creating flexible, extendable, and maintainable systems. Each principle helps avoid common problems in code design:
- S (Single Responsibility Principle) — A class should have only one reason to change.
- O (Open/Closed Principle) — Software entities should be open for extension but closed for modification.
- L (Liskov Substitution Principle) — Subtypes must be substitutable for their base types without altering the correctness of the program.
- I (Interface Segregation Principle) — Clients should not be forced to depend on interfaces they do not use.
- D (Dependency Inversion Principle) — High-level modules should not depend on low-level modules; both should depend on abstractions.
Example in C# for the Single Responsibility Principle:
class ReportGenerator
{
public string GenerateReport() {
// report generation logic
}
}
class ReportPrinter
{
public void Print(string report) {
// report printing logic
}
}
Here, report generation and printing are separated into different classes, which complies with SRP.