Wednesday 4 October 2023


 SOLID Principles in Software Design

Introduction:

In the ever-evolving landscape of software development, writing code that is not only functional but also maintainable, scalable, and adaptable is of paramount importance. This is where solid principles come into play.

As software projects grow in complexity, maintaining and extending the codebase becomes increasingly challenging. This is where software design principles step in to provide a structured approach to creating software that is easier to manage, modify, and extend.

SOLID principles, introduced by Robert C. Martin, offer a set of guidelines that can significantly enhance the quality of your code and overall software architecture

SOLID PRINCIPLES :

SOLID principles are object-oriented design concepts relevant to software development. SOLID is an acronym for five other class-design principles.

These principles were aimed at creating more robust and flexible software systems.

They are 

       Single responsibility principle

       Open-closed principle

       Liskov substitution principle

       Interface segregation principle

       Dependency inversion principle

Single Responsibility Principle :

The single responsibility principle states that every java class must perform a single functionality.

Implementation of multiple functionalities in a single class mashup the code and if any modification is required may affect the whole class.

This principle makes code precise and it can be easily maintained.

Testing is made easier when the single responsibility principle is implemented.

Simple code snippet to illustrate SRP.


 Open-Closed Principle :

The open-closed principle states that according to new requirements the module should be open for extension but closed for modification.

Loose coupling occurs as a result of the added abstraction in this principle.

In this principle extending the class can be done in a variety of ways, including

  • inheriting from class.
  • overwriting the required behaviour from the class.
  • extending certain behaviour of the class. 

Simple code snippet to illustrate OCP:



 Liskov substitution principle:

The Liskov substitution principle was introduced by barbara liskov.

The liskov substitution principle states that it applies to inheritance in such a way that the derived classes must be completely substitutable for their base classes.

In other words if class a is a subtype of class b, we should be able to replace b with a without disrupting the behavior of our program.

The benefits of LSP, like improving code reusability and consistency.

Simple code snippet to illustrate LSP:




Interface segregation principle:

The interface segregation principle states that clients should not be forced to depend upon interface members they do not use.

Abilities that must follow in this principle are

  • Avoiding unnecessary dependencies
  • Smaller, focused interfaces

This improves more flexibility and reduced coupling.

In this Clients can Implement only the Interfaces relevant to their requirements

Simple code snippet to illustrate ISP:




Dependency inversion principle:

The dependency inversion principle states that high-level modules should not depend upon low-level modules; they should depend on abstractions.

DIP focuses on reducing tight coupling between modules, promoting flexibility and easier maintenance.

DIP contributes to the adaptability and extensibility of software.

DIP allows for more efficient code reuse and modular development.

Simple code snippet to illustrate DIP:



Benefits of solid principles :

when you use all the principles of S.O.L.I.D in a combined manner, it becomes easier for you to develop software that can be managed easily.

  • Clean: solid principles make code clean and standard code.
  • Maintainable: with the help of solid principles our code becomes more manageable and easy to maintain.
  • Scalable: easy to refactor or change code.
  • Redundancy: solid principles avoid redundant code.
  • Testable: can be easily unit tested.
  • Readable: solid principles make the code easy and readable.
  • Independent: code becomes independent by reducing dependencies.
  • Reusable: code becomes reusable.


Thank you,

k.vijay(Intern)

vijay.keradhi@eminds.ai

Enterprise Minds. 


                                                                                                                                              

No comments:

Post a Comment

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The focu...