avatarEmanuel Trandafir

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2212

Abstract

id="c8ac">On the other hand, if we extend a base class and our implementation will return a different result or cause unexpected side effects, this would be a violation of Liskov’s Substitution Principle.</p><figure id="6e4f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zBKzBIJZTIrwAZ1NuBiAaA.png"><figcaption>a clear violation of Liskov’s Substitution Principle</figcaption></figure><h2 id="ce6e">4. Interface Segregation Principle</h2><p id="947d">To conform to the Interface Segregation Principle, a client should never depend on functions that it’s not going to use.</p><p id="0ae7">Let’s assume we have an interface with the methods <i>a(…)</i>,<i> b(…)</i>, and <i>c(…). </i>If there are clients only using the methods <i>a(…)</i> and <i>b(…), </i>we are violating the Interface Segregation Principle. A solution would be to break the existing interface into smaller ones.</p><p id="4822">Moreover, suppose we implement this interface and do provide implementations for <i>a(…) </i>and <i>b(…)</i> but not for <i>c(…)</i>, it means, yet again, that there will be clients not using <i>c(...) </i>— therefore we are violating the principle.</p><h2 id="02ca">5. Dependency Inversion Principle</h2><p id="e005">According to the Dependency Inversion Principle, high-level modules should not depend on low-level modules. Details should depend upon abstractions.</p><p id="a7ff">For instance, we can imagine having a <i>payment </i>module and various classes for concrete payment implementations. The various implementations should depend upon the payment module to conform to the Dependency Inversion Principle.</p><p id="faac">As a result, a change in one of the concrete payment implementations (the “<i>details</i>”), won’t affect the high-level <i>payment</i> module (or any other component).</p><figure id="0fdf"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DYeh7OKkCR-XTyEJmssFmg.png"><figcaption></figcaption></figure><h2 id="84e4">Conclusion</h2><p id="e823">In this article, we briefly described each of the five SOLID principles.</p><p id="42eb">The SOLID principles teach us how to build classes that are not rigid, fragile, or non-reusable.</p><p id="3dca">

Options

<b>If we look at the bigger picture, these concepts will be used when designing the components or modules of our system. If you want to dive deeper into this subject, read the article about <a href="https://levelup.gitconnected.com/3-component-cohesion-principles-explained-for-a-junior-developer-53576d1032b3">Component Cohesion Principles</a> — they are the “<i>older brothers</i>” of SOLID.</b></p><p id="2e30">On the other hand, if you want to learn more about SOLId, I highly recommend watching this talk by Uncle Bob.</p><p id="9f5c">After a fairly long introduction, he is explaining what an O.O.P. language is and how encapsulation, inheritance, and polymorphism work.</p><p id="3f39">After that, he shows how to use the SOLID principles to get control over the dependency structure of your system and why this is important.</p> <figure id="f2a7"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FzHiWqnTWsn4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DzHiWqnTWsn4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FzHiWqnTWsn4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854"> </div> </div> </figure></iframe></div></div></figure><h1 id="3499">Level Up Coding</h1><p id="58c1">Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://levelup.gitconnected.com/?utm_source=pub&amp;utm_medium=post">Level Up Coding publication</a></li><li>🔔 Follow us: <a href="https://twitter.com/gitconnected">Twitter</a> | <a href="https://www.linkedin.com/company/gitconnected">LinkedIn</a> | <a href="https://newsletter.levelup.dev">Newsletter</a></li></ul><p id="d897">🚀👉 <a href="https://jobs.levelup.dev/talent/welcome?referral=true"><b>Join the Level Up talent collective and find an amazing job</b></a></p></article></body>

Each of the SOLID Principles Explained in Less Than 20 Seconds

S.O.L.I.D. is the acronym for a set of five O.O.P. design principles that will help you avoid writing fragile, rigid, or non-reusable software.

In this article, we’ll briefly describe each of them and we’ll see how they can help us control the dependency structure of our application.

1. Single Responsibility Principle

The Single Responsibility Principle states that classes or components should have a single source of change.

For instance, if a business use case regarding payments changes, only the functions from the dedicated component should be affected. If any other component or module needs changes, they do not conform to the Single Responsibility Principle.

a Penknife does not conform to the Single Responsibility Principle

2. Open/Closed Principle

According to the Open/Closed Principle, a class or component should be opened for extension and closed for change.

If we follow this principle, we should be able to add a new feature just by extending the component — without touching any of the existing code.

Have you ever noticed how senior developers refactor the existing code before adding a new feature? Initially, they make sure the code conforms to the Open/Closed Principle. After that, they add the new feature without further touching the old code.

3. Liskov Substitution Principle

Liskov Substitution Principle states that a derived class should be able to substitute its parent without the client knowing.

For example, if our function returns a List we have the freedom of changing it internally from a LinkedList to a HashList without breaking the code that’s using it.

On the other hand, if we extend a base class and our implementation will return a different result or cause unexpected side effects, this would be a violation of Liskov’s Substitution Principle.

a clear violation of Liskov’s Substitution Principle

4. Interface Segregation Principle

To conform to the Interface Segregation Principle, a client should never depend on functions that it’s not going to use.

Let’s assume we have an interface with the methods a(…), b(…), and c(…). If there are clients only using the methods a(…) and b(…), we are violating the Interface Segregation Principle. A solution would be to break the existing interface into smaller ones.

Moreover, suppose we implement this interface and do provide implementations for a(…) and b(…) but not for c(…), it means, yet again, that there will be clients not using c(...) — therefore we are violating the principle.

5. Dependency Inversion Principle

According to the Dependency Inversion Principle, high-level modules should not depend on low-level modules. Details should depend upon abstractions.

For instance, we can imagine having a payment module and various classes for concrete payment implementations. The various implementations should depend upon the payment module to conform to the Dependency Inversion Principle.

As a result, a change in one of the concrete payment implementations (the “details”), won’t affect the high-level payment module (or any other component).

Conclusion

In this article, we briefly described each of the five SOLID principles.

The SOLID principles teach us how to build classes that are not rigid, fragile, or non-reusable.

If we look at the bigger picture, these concepts will be used when designing the components or modules of our system. If you want to dive deeper into this subject, read the article about Component Cohesion Principles — they are the “older brothers” of SOLID.

On the other hand, if you want to learn more about SOLId, I highly recommend watching this talk by Uncle Bob.

After a fairly long introduction, he is explaining what an O.O.P. language is and how encapsulation, inheritance, and polymorphism work.

After that, he shows how to use the SOLID principles to get control over the dependency structure of your system and why this is important.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

Software Development
Python
Java
Programming
JavaScript
Recommended from ReadMedium