SOLID Design Principles with Implementation in C#

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.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read