Sobes.tech
Junior — Middle

Could you explain the main principles underlying 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 code:

  • S (Single Responsibility Principle) — A class should have only one reason to change, meaning it should perform only one task.
  • 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) — It's better to have several specific interfaces than a single general-purpose interface.
  • D (Dependency Inversion Principle) — High-level modules should not depend on low-level modules; both should depend on abstractions.

Example in Python for the Single Responsibility Principle:

class ReportGenerator:
    def generate(self):
        # generates a report
        pass

class ReportSaver:
    def save(self, report):
        # saves the report to a file
        pass

Here, report generation and saving are separated into different classes.