What
are SOLID Design Principles?
SOLID
is an acronym that stands for:
- Single
Responsibility Principle (SRP)
- Open-Closed
Principle (OCP)
- Liskov
Substitution Principle (LSP)
- Interface
Segregation Principle (ISP)
- Dependency
Inversion Principle (DIP)
- Single
Responsibility Principle (SRP):
- Definition: A class should have only one
reason to change, meaning that it should have only one responsibility or
job.
- Implications:
- Each
class should encapsulate a single functionality or responsibility.
- A class should not be forced to change for more than one reason.
- Open/Closed Principle (OCP):
- Definition: Software entities (classes,
modules, functions, etc.) should be open for extension but closed for
modification.
- Implications:
- New
functionality should be added through extensions (e.g., inheritance,
interfaces) without modifying existing code.
- Existing
code should be closed for modification to prevent unintended side
effects.
- Liskov
Substitution Principle (LSP):
- Definition: Subtypes must be
substitutable for their base types without altering the correctness of
the program.
- Implications:
- Derived
classes should extend the behavior of the base class without changing
its contract.
- Code
that uses a base class should be able to use its derived classes without
knowing it.
- Interface
Segregation Principle (ISP):
- Definition: A class should not be forced
to implement interfaces it does not use. Clients should not be forced to
depend on interfaces they do not use.
- Implications:
- Interfaces
should be small and specific to the needs of the classes that implement
them.
- Clients
should not be burdened with methods they do not need.
- Dependency
Inversion Principle (DIP):
- Definition: 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.
- Implications:
- Depend
on abstractions (e.g., interfaces) rather than concrete implementations.
- High-level
modules and low-level modules should both depend on the same
abstractions.
No comments:
Post a Comment