SOLID


Single Responsibility Principle (SRP)

A class should have only one responsibility or reason to change.
Example:
If a class handles both data storage and formatting, changes to one may break the other. Instead, each responsibility should be assigned to separate classes.

Open-Closed Principle (OCP)

A class should be open for extension but closed for modification. You should be able to add new functionality without changing existing code.
Example:
Instead of modifying a checkout function to support new payment methods, extend it by adding new classes.

Liskov Substitution Principle (LSP)

Subclasses should be usable in place of their base classes. Derived classes must maintain the behavior expected from the base class.
Example:
If a subclass introduces behavior that breaks existing code relying on the base class, it violates LSP.

Interface Segregation Principle (ISP)

Classes should not be forced to depend on methods they do not use.
Example:
Instead of one large interface, split it into multiple smaller ones to ensure that each class only implements what it needs.

Dependency Inversion Principle (DIP)

High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not rely on details, but details should rely on abstractions.
Example:
If you have multiple event loggers (e.g., file and database loggers), they should depend on a common abstraction to make switching between them easy.