SOLID Principles
In this post, I am going to discuss SOLID principles, yes I mean the holy grail of OOP that empower developers to produce better software.
Let's start with the definition. SOLID Principles is an acronym for five design principles intended to make software design more understandable, flexible and maintainable; including Single Responsibility principle, Open-Closed principle, Liskov Substitution principle, Interface Segregation principle and Dependency Inversion principle.
The consequence of following SOLID principles will lead developers towards more extendable, logical and easier to read software; and even thought it seems like an irony to have a SOLID software but we all hope to these principles guide us toward more understandable, flexible and maintainable software. Now let's get into the principles' details.
Single Responsibility Principle (SRP)
According to Robert C. Martin, It is people who request changes and hence the reasons for change are people. you don’t want to confuse those people, or yourself, by mixing together the code that many different people care about for different reasons.
SRP increases the cohesion between things that change for the same reasons, and decrease the coupling between those things that change for different reasons.
The following code snippet is an example of breaking SRP, since CustomerOrderService class is responsible for calculating, saving and also sending order email.
Open-Closed Principle(OCP)
https://en.wikipedia.org/wiki/Liskov_substitution_principle
https://en.wikipedia.org/wiki/Interface_segregation_principle
https://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html
https://hackernoon.com/solid-principles-made-easy-67b1246bcdf
https://www.codeproject.com/Tips/1033646/SOLID-Principle-with-Csharp-Example
Further Readings
https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start
https://www.c-sharpcorner.com/UploadFile/damubetha/solid-principles-in-C-Sharp/
https://code.tutsplus.com/tutorials/solid-part-3-liskov-substitution-interface-segregation-principles--net-36710
https://martinfowler.com/articles/dipInTheWild.html
A class should have only a single responsibility, that is, only changes to one part of the software's specification should be able to affect the specification of the class.In other words, Software module should have one and only one reason to change. Now the question is: What defines a reason to change?
According to Robert C. Martin, It is people who request changes and hence the reasons for change are people. you don’t want to confuse those people, or yourself, by mixing together the code that many different people care about for different reasons.
SRP increases the cohesion between things that change for the same reasons, and decrease the coupling between those things that change for different reasons.
The following code snippet is an example of breaking SRP, since CustomerOrderService class is responsible for calculating, saving and also sending order email.
Open-Closed Principle(OCP)
A module should be open for extension, but closed for modification.OCP is achieved through Abstractions and Polymorphism. A simple way to apply OCP is to implement the new functionality on new derived (sub)classes that inherit the original class implementation. Another way is to mediate client access to the original class with an abstract interface, so new functionality can be implemented on new classes that are accessed through the same interface.
The following snippet demonstrates an example Employee class that violates OCP principle as well as an approach to avoid the violation.
Liskov Substitution Principle(LSP)
Liskov's principle imposes some standard requirements on signatures:
Subclasses should be substitutable for their base classes.LSP is a way of ensuring that inheritance is used correctly such that inheritance instead of "IS-A" relationship is a "IS-SUBSTITUTABLE-FOR" relationship.
Liskov's principle imposes some standard requirements on signatures:
- Contravariance of method arguments in the subtype.
- Covariance of return types in the subtype.
- No new exceptions should be thrown by methods of the subtype, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the supertype.
- Preconditions cannot be strengthened in a subtype.
- Postconditions cannot be weakened in a subtype.
- Invariants of the supertype must be preserved in a subtype.
- History constraint (the "history rule"). A violation of this constraint can be exemplified by defining a mutable point as a subtype of an immutable point. In the history of the immutable point, the state is always the same after creation, so it cannot include the history of a mutable point in general.
Interface Segregation Principle(ISP)
Many client-specific interfaces are better than one general-purpose interface.ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them. In another word, correct abstraction is the key to Interface Segregation Principle. The following code snippet shows an example of ISP principle violation and a solution to avoid the violation.
Dependency Inversion Principle (DIP)
Depend upon abstractions, do not depend on concretions.
- High level modules should not depend upon low level modules. Both should depend upon abstractions.
- Abstractions should not depend upon details. Details should depend upon abstractions.
References
https://en.wikipedia.org/wiki/SOLIDhttps://en.wikipedia.org/wiki/Liskov_substitution_principle
https://en.wikipedia.org/wiki/Interface_segregation_principle
https://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html
https://hackernoon.com/solid-principles-made-easy-67b1246bcdf
https://www.codeproject.com/Tips/1033646/SOLID-Principle-with-Csharp-Example
Further Readings
https://sites.google.com/site/unclebobconsultingllc/getting-a-solid-start
https://www.c-sharpcorner.com/UploadFile/damubetha/solid-principles-in-C-Sharp/
https://code.tutsplus.com/tutorials/solid-part-3-liskov-substitution-interface-segregation-principles--net-36710
https://martinfowler.com/articles/dipInTheWild.html
Thank you so much for all the wonderful information about Technology! I love your work.
ReplyDeletejavascript training in chennai
js class
Hibernate Training in Chennai
core java training in chennai
Spring Training in Chennai
QTP Training in Chennai
Manual Testing Training in Chennai
JMeter Training in Chennai
The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.
DeleteProjects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.
Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.
ReplyDeleteSuperb ! Your blog is incredible. I am impressed with it. Thanks for sharing this content under this topic.
Seo Training in Bangalore
Best Seo Training in Bangalore
Data Analytics Courses in Bangalore
Data Science Courses in Bangalore
Digital Marketing Courses in Bangalore
German Classes in Bangalore
Python Training in Bangalore
Selenium Training in Bangalore
Nice post. Thanks for sharing.
ReplyDeleteSpoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
IELTS Training in Chennai
IELTS Chennai
Best English Speaking Classes in Mumbai
Spoken English Classes in Mumbai
IELTS Mumbai
IELTS Center in Mumbai
IELTS Coaching in T Nagar
This is good information and really helpful for the people who need information about this.
ReplyDeletecontent writing course in chennai
content writing training in chennai
German Classes in Chennai
IELTS Coaching in Chennai
Japanese Classes in Chennai
Spoken English Classes in Chennai
TOEFL Coaching in Chennai
German Classes in Anna Nagar
Spoken English Classes in Anna Nagar
The blog you shared is very good. I expect more information from you like this blog. Thank you.
ReplyDeleteWeb Designing Course in chennai
Web Designing Course in bangalore
web designing course in coimbatore
web designing training in bangalore
web designing course in madurai
Web Development courses in bangalore
Web development training in bangalore
Salesforce training in bangalore
Python training in Bangalore
Web Designing Course in bangalore with placement
Thank you so much for updating the useful blog. I liked this blog..
ReplyDeleteSpoken English Classes in Bangalore
Spoken English Classes in Chennai
Spoken English Classes in BTM
Spoken English Classes in Marathahalli
Spoken English Classes near Marathahalli
Spoken English Marathahalli
DevOps Training in Bangalore
PHP Training in Bangalore
Data Science Courses in Bangalore
English Speaking Course in Bangalore
"Good post. I learn something new and challenging on sites I stumbleupon on a daily basis. It's always interesting to read content from other writers and practice a little something from their web sites.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery"
very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
Very Nice article, thanks for sharing the article.
ReplyDeletedevops methodology
habits of success
mobile application testing tools
why web development is important
advanced excel interview questions
It’s a nice blog with very useful information!!!
ReplyDeleteWeb Designing Course in chennai
PHP Training in Chennai
Spoken English Classes in Chennai
German Classes in Chennai
Salesforce Training in Chennai
IELTS Coaching in Chennai
Thanks for sharing this blog and very useful.,
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Bangalore
Digital Marketing Online Course
The way of content delivery is good, excellent in formation.
ReplyDeleteBest Digital marketing company in chennai
Digital Marketing Company in chennai
SEO Company in Chennai
Thanks for providing great informatics and looking beautiful blog...
ReplyDeleteHardware and Networking Classes in Bangalore,
IT and IMS training institute.
Visit us https://www.innovativeacademy.in/