SOLID is a set of five design principles that help software developers write maintainable and scalable code. Let's walk through each one.
A class should have only one reason to change. Each class should encapsulate a single piece of functionality.
# Bad: UserService does too many things
class UserService:
def create_user(self, data): ...
def send_email(self, user): ...
def generate_report(self, user): ...
# Good: Each class has one responsibility
class UserService:
def create_user(self, data): ...
class EmailService:
def send_email(self, user): ...
class ReportService:
def generate_report(self, user): ...
Software entities should be open for extension but closed for modification. Use abstractions to allow new behavior without changing existing code.
Subtypes must be substitutable for their base types without altering the correctness of the program.
Clients should not be forced to depend on interfaces they do not use. Prefer many small, focused interfaces over one large one.
High-level modules should not depend on low-level modules. Both should depend on abstractions.
These principles form the foundation of good object-oriented design and are frequently discussed in system design interviews.