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)
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)
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.
In addition to the signature requirements, the subtype must meet a number of behavioral conditions (design by contract methodology):
  • 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.
The following code shows an example of LSP violation named Square/Rectangle dilemmas.

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.
The following code snippet demonstrates a DIP violation example since CustomerController class depends on CustomerService and DbLogger concrete classes; and a solution to avoid the violation.


  1. Replies
    1. 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.

      Projects 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.

  2. "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.
    Digital 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"

  3. Thanks for providing great informatics and looking beautiful blog...
    Hardware and Networking Classes in Bangalore,
    IT and IMS training institute.

    Visit us


Post a Comment

Popular Posts