SOLID Design Principles with Implementation in C#

WEBINAR:
On-Demand

Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js


Introduction

In Object Oriented Programming (OOP) concepts, we should have low coupling, high cohesion, and strong encapsulation. The SOLID principles of OOPS help developers to achieve scalability by applying these principles together, so that we are able to write better quality and more robust code. The system created by following this principle becomes easy to maintain, reusable, and easily extendable over time. SOLID principles were introduced by Michael Feathers for five principles.

What Is the SOLID Principle?

SOLID principles are conveniently remembered by using the English word SOLID. Each letter in this acronym has a meaning:

  • S stands for SRP (Single responsibility principle
  • O stands for OCP (Open closed principle)
  • L stands for LSP (Liskov substitution principle)
  • I stands for ISP (Interface segregation principle)
  • D stands for DIP (Dependency inversion principle)

Single Responsibility Principle (SRP)

As per the SRP principle, a class should be responsible for doing one thing. That means everything in that class should be related to a single purpose. This doesn't mean it should only have one method, but instead all the methods should relate to a single purpose. The Single Responsibility Principle gives us an opportunity to identify classes at the design phase of an application. The advantage of the Single Responsibility Principle is that, as the size of the classes become shorter making code easily understandable, the class is easier to maintain and the class is more reusable.

Open Closed Principle (OSP)

The Open Closed Principle says, change a class behavior using inheritance and composition. That means, we need to design our class in such a way that the new functionality can be added easily when new requirements are generated. The Closed Principle means, if we have already developed a class and it has passed through all unit testing, the developer should not change code after that until we find bugs. OSP also suggests, open for extensibility but closed for modification. If a class is being used by other clients, that class should be open for extensibility. To make a class open for extensibility, add a virtual keyword to the methods of the class.

Liskov Substitution Principle (LSP)

The Liskov Substitution Principle (LSP) talks about polymorphism. It ensures that a derived class does not affect the behavior of the parent class. As per LSP, you should be able to use any derived class instead of a parent class and have it behave in the same manner, without modification. LSP is just an extension of the Open Close Principle and it means that we must ensure that new derived classes extend the base classes without changing their behavior.

Interface Segregation Principle (ISP)

As per the Interface Segregation Principle, clients should not depend on methods they do not use. Instead, of one "fat" interface, it's preferred to base on groups of methods. An interface should be more closely related to the code that uses it so the methods on the interface are defined by which methods the client code needs. The larger the interface, the more likely it includes methods that not all implementers can do. That's the essence of the Interface Segregation Principle.

Interfaces are introduced to achieve loose coupling. It's not the classes that use the interfaces, but clients are the one that use interfaces.

Dependency Inversion Principle (DIP)

The Dependency Inversion Principle says high-level modules should not be dependent on low-level modules. Both should depend on abstractions. If a class knows explicitly about the design and implementation of another class, it raises the risk that it breaks the other class. So, it's suggested to keep these high-level and low-level modules and classed loosely coupled as much as possible.

Dry and Wet Coding

Don't Repeat Yourself (DRY) is a software development principle that says lighter and easier to maintain, manipulate, single, unambiguous, authoritative representation, and store. On the other hand, Write Everything Twice (WET) is just the opposite of the DRY principle. It's always suggested to follow DRY principles and violations of DRY are referred to as WET solutions.

Conclusion

I hope you have understood the five SOLID principles after reading through this article. SOLID principles of object-oriented programming allow us to write structured and neat code that is easy to extend and maintain. Because of the SOLID principles, your code might become larger but the quality will be good. So, eventually understanding the SOLID principles will help you to write better code.



About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Comments

  • Misleading Title

    Posted by Chris on 02/19/2018 11:15am

    I'm not seeing any c# implementation that the title suggests. Am I missing something? Or have you missed a link? Without the promised implementation it's a pretty bog standard article and no better than Wikipedia

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date