
What is a SOLID? How to use for better code?
Introduction to SOLID principles in object-oriented programming
I. Revise OOP concepts
Objct-oriented programming (OOP) is one of the most widely used programming paradigms. The special properties that make object-oriented programming effective are:
Abstraction
: Creating abstract classes to model objects in the real world.Encapsulation
: The entities of an abstract class have distinct attribute values.Inheritance
: Objects can easily inherit and extend each other.Polymorphism
: An action can be performed in many different ways depending on the specific type of object being called.
These special properties of OOP help us build programs that solve many different specific problems in the real world. Most programmers are already familiar with these properties of OOP, but not everyone knows how to combine these properties to increase the efficiency of the application. One of the guidelines to help us use OOP more effectively is the SOLID principles.
II. What is SOLID?
SOLID is an acronym for the first five principles of object-oriented design. It helps programmers write code that is easy to read, understand, and maintain. It was introduced by Robert C. Martin and Michael Feathers. These five principles include:
III. Example
For example, with a computer hard drive, you can use a new generation SSD solid-state drive to run faster, but you can also use a regular HDD spinning drive. The motherboard manufacturer has no way of knowing whether you will use an SSD or a regular spinning disk HDD. However, they will always ensure that you can use whatever you want, as long as the hard drive has a SATA interface standard to be able to be attached to the motherboard. Here, the SATA interface standard is the interface, and SSD or spinning disk HDD is the specific implementation.
In programming, when applying this principle, in high-level abstract classes, we often use interfaces more than a specific inheritance type. For example, to connect to the Database, we often design the DataAccess abstract class with common methods such as save(), get(), ... Then, depending on which type of DBMS is used (eg: MySql, MongoDB, ...), we inherit and implement these methods. The polymorphic nature of OOP is heavily used in this principle.
IV. Conclusion
Clear, Understand
:SOLID
are 5 basic principles in software design. It helps us organize functions, methods, classes more accurately. How to connect components, modules together.Teamwork
: ApplySOLID
in your work, you will create better, easier-to-understand functions. Helping you and your colleagues read each other's code better.Changeable
:SOLID
helps create clear, coherent, highly independent modules and classes. Therefore, when there is a request for change or expansion from customers, we do not have to spend too much effort to make the change.Reusable
:SOLID
makes programmers think more about how to write software, so the code written will be coherent, easy to understand, and easy to use.
🍃 Related posts
Want to update?
Subscribe to my blog to receive the latest updates from us.