Skip to main content

SOLID - Principles of Object Oriented Class Design

The theory of SOLID principles was introduced by Martin in his 2000 paper "Design Principles and Design Patterns". The SOLID acronym was introduced later by Michael Feathers.

1. Single responsibility principle
A class should have only one reason to change

Martin defines a responsibility as a reason to change, and concludes that a class or module should have one, and only one, reason to be changed (i.e. rewritten).

"When designing our classes, we should aim to put related features together, so whenever they tend to change they change for the same reason. And we should try to separate features if they will change for different reasons." - Steve Fenton

2. Open-Closed Principle
A module should be open for extension but closed for modification.

Of all the principles of object oriented design, this is the most important.

It means simply this: We should write our modules so that they can be extended, without requiring them to be modified. In other words, we want to be able to change what the modules do, without changing the source code of the modules.

3. Liskov Substitution Principle
Subclasses should be substitutable for their base classes

This principle was coined by Barbar Liskov in her work regarding data abstraction and type theory. It also derives from the concept of Design by Contract (DBC) by Bertrand Meyer

Derived classes should be substitutable for their base classes. That is, a user of a base class should continue to function properly if a derivative of that base class is passed to it.

4. Interface Segregation Principle
Many client specific interfaces are better than one general purpose interface

The essence of the principle is quite simple. If you have a class that has several clients, rather than loading the class with all the methods that the clients need, create specific interfaces for each client and multiply inherit them into the class.

5. Dependency Inversion Principle
Depend upon Abstractions. Do not depend upon concretions

Dependency Inversion is the strategy of depending upon interfaces or abstract functions and classes, rather than upon concrete functions and classes. This principle is the enabling force behind component design, COM, CORBA, EJB, etc.


Comments

Popular posts from this blog

Team Work

Ramo Vigrahavan Dharmah

A great verse from Ramayana: आहार निद्रा भय मैथुनं च  सामान्यमेतत् पशुभिर्नराणाम् । धर्मो हि तेषामधिको विशेष:  धर्मेण हीनाः पशुभिः समानाः ॥ AhAra-nidrA-bhaya-maithunam cha samAnam_etat_pashubhir_narANAm | dharmo hi teShAm adhiko visheSho dharmeNa hInAH pashubhiH samAnAH|| Eating, Sleep, Fear and Sex ; these habits are common between human beings and animals. It is the Dharma which is the special quality of the human beings. Without the Dharma, they are similar to the animals.  Rama is The Embodiment Of Dharma.  Maaricha, while speaking to Ravana- (Aranya Kandam 37-13): रामो विग्रहवान् धर्मः साधुः सत्य पराक्रमः | राजा सर्वस्य लोकस्य देवानाम् इव वासवः || ३-३७-१३|| raamo vigrahavaan dharmaH saadhuH satya paraakramaH | raajaa sarvasya lokasya devaanaam iva vaasavaH || 3-37-13 "Rama is the embodiment of righteousness, he is an equable person with truthfulness as his valour, and as with Indra to all gods he is the king of entire world. [3-37-13] Another great phra

Story about Samsara from Vidura in Maha Bharatha

From Sri Maha Bharatham - 11th Maha Parva (Stri Parva) - Jalapradanika Sub Parva - 5th Chapter "Dhritarashtra said, ‘Tell me in detail everything about the ways of that intelligence by which this wilderness of duties may be safely covered.’ "Vidura said, ‘Having bowed down to the Self-create, I will obey thy behest by telling thee how the great sages speak of the wilderness of life. A certain  brahmana,  living in the great world, found himself on one occasion in a large inaccessible forest teeming with beasts of prey. It abounded on every side with lions and other animals looking like elephants, all of which were engaged in roaring aloud. Such was the aspect of that forest that Yama himself would take fright at it. Beholding the forest, the heart of the  brahmana  became exceedingly agitated. His hair stood on end, and other signs of fear manifested themselves, O scorcher of foes! Entering it, he began to run hither and thither, casting his eyes on every point of