SOLID Principles : What I learned!

The SOLID principles are comprised of five individual principles for writing better software, especially in object-oriented languages. Let’s see one by one:

1. Single Responsibility Principle:

What is responsibility exactly means? : It’s a decision our code is making about the specific implementation details of some part of what the application does. Responsibilities in our code represent things that may change at different times and for different reasons. This is closely related to following points:

  • Loose Coupling: Loose coupling refers to approaches that can be used to support having different details of the application interact with one another in a modular fashion.
  • Separation of concerns: suggests that programs should be separated into distinct sections that each address a separate concern or set of information that affects the program.
  • Cohesion: describes how closely related elements of a class or module are to one another. Classes that have many responsibilities will tend to have less cohesion than classes that have a single responsibility.

2. Open-Closed Principle:

Typical Approaches to OCP:

  • Use Inheritance: By using Inheritance we can change the behavior of the underlying type without having to change or even have access to that type simply by creating a new child class that inherits from it.
  • Using composition and injection: Instead of placing logic directly within a class, the logic is provided by another type the class references.

3. Liskov Substitution Principle:

Fixing LSP Violations:

  • Minimise the Null checks of object with new features of C#
  • Use Guard Clauses to check Nulls
  • Whenever implement an interface or inherit from a base class, make sure you fully implement it.

4. Interface Segregation Principle:

  • ISP is related to Liskov substitution principle. Large interfaces are harder to fully implement and, thus, more likely to only be partially implemented and, therefore, to not be substitutable for their base type.
  • The interface segregation principle also relies heavily on cohesion. Small, cohesive interfaces are preferable to large interfaces whose methods are only loosely related to one another.

Fixing ISP Violations:

  • Use Adaptor Design Pattern to create small, cohesive interfaces

5. Dependency Inversion Principle:

Dependancy Injection is technique that is used to allow calling code to inject the dependancies a class needs when it is instantiated. Three Primary Principles:

  • Constructor Injection
  • Property Injection
  • Parameter Injection

Advantages of Dependency Injection :

  • Extensibility
  • Testability
  • Late Binding
  • Parallel Development
  • Maintainability

To see the example of SOLID principle, please visit the source code at : https://github.com/milindchavan12/SOLID

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Milind Chavan

An Azurer, Web developer, Technologist, Writer, Poet, Runner. Opinions are my own.