SOLID Principles
Today I
will be talking about the SOLID principles which is an acronym that stands for
five widely accepted principles of object-oriented programming and design,
these principles will aid us to improve our software design and more important
to have a software with less bugs.
S is for Single Responsibility : This principle states
something that we usually know, but we do not follow as it is. It states that a
class should have exactly one responsibility, it should have exactly one reason
that cause it to be changed.
O is for Open/Closed
Principle: This principle states that a class (or function) must be open for
extension but closed to modification. Adhering Open Closed Principle means that
when adding new behavior you should leave the base classes alone, instead,
create new inheriting classes adding behavior to these instead of the base
class, this way, avoiding the issues that might happen if we modify the base
class.
L is for Liskov Substitution Principle: One of the nice
things about OOP is that you can pass a new class to an existing a function
that has been written to work with the base class and it will still work, LSP
is intended to keep that relationship happy and running. If you follow LSP you
should be free to substitute a child class in a function that expects to deal
the class, however if not, you are violating this principle. In many cases
instead of inheritance, programmers should create classes that are composed of
base classes.
I is for Interface
Segregation Principle: What this principle says is that instead of creating a
big interface with a lot of methods you should create smaller interfaces,
partitioning the work according to what they concern. Classes this way can pick
what they implement rather than having to swallow all or nothing.
D is for Dependency
Inversion Principle: What this principle states is that instead of writing code
that refers to actual classes, you should instead write code that refers to
interfaces or abstract classes.
These are
great principles and we should use them
Comentarios
Publicar un comentario