avatarGerald Nguyen

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

2780

Abstract

ption>EmailService and its associations</figcaption></figure><p id="037c">The <code>ElectricCar</code> adds electric features without modifying the internal of <code>Car</code>.</p><h1 id="2896">Liskov substitution</h1><blockquote id="6ad6"><p>Subclasses should be substitutable for their base classes</p></blockquote><p id="ffeb">In the context of Object-oriented design, a class or method that accepts an instance of a base class should be able to take any instance of its subclass.</p><p id="e611">An <code>OrderService</code> should be able to use any instance that conforms to the interface of <code>EmailService</code> without concerning that the instance’s actual class is <code>EmailService</code> itself or <code>RecallableEmailService</code></p><p id="2d0d">An <code>UrbanTripPlanner</code> should be able to use any instance of <code>Car</code> to determine a route without knowing whether it represents an electric or petrol car.</p><h1 id="1bf5">Interface segregation</h1><blockquote id="3c91"><p>Many client specific interfaces are better than one general purpose interface</p></blockquote><p id="8a61">An <code>OrderService</code> does not need to <code>recallEmail(...)</code> . Hence it only depends on <code>EmailService</code> for sending emails, not <code>RecallableEmailService</code></p><p id="e2be">Similarly, an <code>UrbanTripPlanner</code> does not need the <code>recharge(...)</code> method from an <code>EletricCar</code>. It depends only on the interface provided by <code>Car</code></p><figure id="f7ba"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*AZ1fklqQkAmglfU5NxLC3g.png"><figcaption>Car and its associations</figcaption></figure><h1 id="07e2">Dependency inversion</h1><blockquote id="35bf"><p>Depend upon Abstractions. Do not depend upon concretions.</p></blockquote><p id="09c8">The <i>procedural </i>dependency mechanism starts from the top, gradually expands, and depends on concrete implementation details.</p><p id="e491">In Object-oriented programming, the dependency is <i>inverted</i>. The majority of dependencies should be upon abstractions. And where implementation exists, its relationship with abstraction <i>inverts</i>. Rather than being directly depended on, the implementation now depends on abstraction.</p><figure id="089d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xZNkJr8o1fxcJ2efNggICA.png"><figcaption>Source: Uncle Bob’s Design Principles and Design Patterns</figcaption></figure><p id="e17a">An <code>OrderService</code> depends on the interface provided by <code>EmailService</code> and not its IMAP or POP implementation.</p><p id="48e6">An <code>UrbanTripPlanner</code> depends on the interface provided by <code>Car</code> , not its Tesla or Toyota implementation</p><h1

Options

id="5456">Conclusion</h1><p id="cba1">We have examined the origin of the SOLID acronym, as well as looked at each of them in detail with 2 object-oriented design use cases each.</p><p id="5cae">You may have followed some of these principles in the past without knowing because they are good practices. Encapsulation, in my opinion, is following the <b>Open-close principle</b>.</p><p id="bd1e">You may have also applied some of them because it is the way things are. Syntactically, an <code>ElectricCar</code> object can always pass in for a <code>Car</code> parameter, thereby following the <b>Liskov substitution principle</b>. If you have used Dependency Injection, you already follow the <b>Dependency Inversion principle</b>.</p><p id="5532">Other principles require a bit of thought. The <b>Single Responsibility principle</b> needs a good OO model to keep the method/class/module clean and lean. You may need good judgment to determine how deep the segregation should be while following the <b>Interface Segregation principle</b>.</p><h1 id="2901">References</h1><ul><li><a href="https://web.archive.org/web/20150906155800/http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf">Design Principles and Design Patterns</a> paper</li><li><a href="http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">Principles of OOD</a> article</li><li><a href="https://www.oreilly.com/library/view/clean-architecture-a/9780134494272/">Clean Architecture: A Craftsman’s Guide to Software Structure and Design</a></li><li><a href="https://en.wikipedia.org/wiki/SOLID">https://en.wikipedia.org/wiki/SOLID</a></li></ul><p id="8285">If you like this article, please <a href="https://geraldnguyen.medium.com/subscribe">follow me</a> for more quality content.</p><p id="07f8">Other articles in this series:</p><ul><li><a href="https://readmedium.com/becoming-a-solid-developer-7f0e0ab359f6">Becoming a SOLID developer</a></li><li><a href="https://readmedium.com/solid-in-action-the-single-responsibility-principle-7cb70c32cc03">SOLID in Action — the Single Responsibility Principle</a></li><li><a href="https://readmedium.com/solid-in-action-the-open-closed-principle-5b8d09a60a5a">SOLID in Action — the Open-closed principle</a></li><li><a href="https://readmedium.com/solid-in-action-the-liskov-substitution-principle-4b1868ad81fd">SOLID in Action — the Liskov Substitution principle</a></li><li><a href="https://readmedium.com/solid-in-action-the-interface-segregation-principle-6c8f92d2133a">SOLID in Action — the Interface Segregation principle</a></li><li><a href="https://readmedium.com/solid-in-action-the-dependency-inversion-principle-5567bddd6cfc">SOLID in Action — the Dependency Inversion principle</a></li></ul><p id="2b77">Thank you.</p></article></body>

Software Development

Becoming a SOLID developer

By following SOLID software engineering principles

Being called solid is a compliment. The word carries the positive notions of reliability and respectability. All of us should aim to become one.

https://www.collinsdictionary.com/dictionary/english/solid (screenshot)

If you are a software engineer or want to become one, you are in luck because there is a set of SOLID principles to guide your way.

SOLID is an acronym in the software engineering profession. It stands for:

  • Single Responsibility principle
  • Open-closed principle
  • Liskov Substitution principle
  • Interface segregation principle
  • Dependency inversion principle

The term was coined by Michael Feather around 2004 for the 5 object-oriented design principles first collected by Uncle Bob in his 2000 paper Design Principles and Design Patterns and his other articles and books

Let’s look at these principles in more detail.

Single Responsibility

Each class should have a single responsibility.

An EmailService should just concern itself with sending emails, not with sending emails and updating order status.

A Car class should be about cars only, not other types of motorized vehicles. If we need to describe an electric car, then a subclass such asElectricCar is more appropriate than overloading Car with electric and petrol car characteristics.

Open-close principle

A module should be open for extension but closed for modification

The EmailService can be extended by a RecallableEmailService which adds recall-ability feature to the email just sent.

EmailService and its associations

The ElectricCar adds electric features without modifying the internal of Car.

Liskov substitution

Subclasses should be substitutable for their base classes

In the context of Object-oriented design, a class or method that accepts an instance of a base class should be able to take any instance of its subclass.

An OrderService should be able to use any instance that conforms to the interface of EmailService without concerning that the instance’s actual class is EmailService itself or RecallableEmailService

An UrbanTripPlanner should be able to use any instance of Car to determine a route without knowing whether it represents an electric or petrol car.

Interface segregation

Many client specific interfaces are better than one general purpose interface

An OrderService does not need to recallEmail(...) . Hence it only depends on EmailService for sending emails, not RecallableEmailService

Similarly, an UrbanTripPlanner does not need the recharge(...) method from an EletricCar. It depends only on the interface provided by Car

Car and its associations

Dependency inversion

Depend upon Abstractions. Do not depend upon concretions.

The procedural dependency mechanism starts from the top, gradually expands, and depends on concrete implementation details.

In Object-oriented programming, the dependency is inverted. The majority of dependencies should be upon abstractions. And where implementation exists, its relationship with abstraction inverts. Rather than being directly depended on, the implementation now depends on abstraction.

Source: Uncle Bob’s Design Principles and Design Patterns

An OrderService depends on the interface provided by EmailService and not its IMAP or POP implementation.

An UrbanTripPlanner depends on the interface provided by Car , not its Tesla or Toyota implementation

Conclusion

We have examined the origin of the SOLID acronym, as well as looked at each of them in detail with 2 object-oriented design use cases each.

You may have followed some of these principles in the past without knowing because they are good practices. Encapsulation, in my opinion, is following the Open-close principle.

You may have also applied some of them because it is the way things are. Syntactically, an ElectricCar object can always pass in for a Car parameter, thereby following the Liskov substitution principle. If you have used Dependency Injection, you already follow the Dependency Inversion principle.

Other principles require a bit of thought. The Single Responsibility principle needs a good OO model to keep the method/class/module clean and lean. You may need good judgment to determine how deep the segregation should be while following the Interface Segregation principle.

References

If you like this article, please follow me for more quality content.

Other articles in this series:

Thank you.

Software Engineering
Solid Principles
Uncle Bob
Object Oriented
Recommended from ReadMedium