Return to site

SOLID PRINCIPLES: Single Responsibility ๐Ÿ’ช๐Ÿ‘จโ€๐Ÿ’ป

ยท coding,solid,java,devops

SOLID principles basically form the fundamental guidelines for buildingย 

๐Ÿ‘‰ object-oriented ,ย 

๐Ÿ‘‰ loosely coupled ,ย 

๐Ÿ‘‰ robust, maintainable andย 

๐Ÿ‘‰ easily understandable applications .

๐€ ๐œ๐ฅ๐š๐ฌ๐ฌ ๐ฌ๐ก๐จ๐ฎ๐ฅ๐ ๐ก๐š๐ฏ๐ž ๐จ๐ง๐ž ๐š๐ง๐ ๐จ๐ง๐ฅ๐ฒ ๐จ๐ง๐ž ๐ซ๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ข๐›๐ข๐ฅ๐ข๐ญ๐ฒ.ย 

We should write, change or maintain a class for only one purposeย 

which gives us the advantage of cleaner ,robust code andย 

fewer test cases as to we know which functionality is covered in which class and which class would be modified in case of a change.ย 

For ex:

๐‘๐‘ข๐‘๐‘™๐‘–๐‘ ๐‘๐‘™๐‘Ž๐‘ ๐‘  Calculator{

๐‘๐‘ข๐‘๐‘™๐‘–๐‘ ๐‘ฃ๐‘œ๐‘–๐‘‘ ๐‘๐‘Ÿ๐‘–๐‘›๐‘ก๐ท๐‘’๐‘ก๐‘Ž๐‘–๐‘™๐‘ () {}

๐‘๐‘ข๐‘๐‘™๐‘–๐‘ ๐‘‘๐‘œ๐‘ข๐‘๐‘™๐‘’ ๐‘๐‘Ž๐‘™๐‘๐‘ข๐‘™๐‘Ž๐‘ก๐‘’() {}

๐‘๐‘ข๐‘๐‘™๐‘–๐‘ ๐‘ฃ๐‘œ๐‘–๐‘‘ ๐‘Ž๐‘‘๐‘‘๐‘ก๐‘œ๐ท๐ต() {}

}

Above class has three separate responsibilities:ย 

โ€•printing,ย 

โ€•calculation andย 

โ€•adding to database.ย 

By applying SRP, we can separate the above class into three classes with separate responsibilities.