Skip to main content

Single Responsibility Principle

Single Responsibility Principle

What are S.O.L.I.D Principles?

Simply , S.O.L.I.D principles are five object oriented principles by Robert.C Martin that should be followed when designing a software.

SSingle Responsibility
O Open Close Principle
L →  Liskov Substitution
→   Interface Segregation
D Dependency Inversion

Why developers should know about these principles?

The broad goal of the SOLID principles is to reduce dependencies so that engineers change one area of software without impacting others. Additionally, they’re intended to make designs easier to understand, maintain, and extend. Ultimately, using these design principles makes it easier for software engineers to avoid issues and to build adaptive, effective, and agile software.

(https://www.bmc.com/blogs/solid-design-principles/#:~:text=The%20SOLID%20principles%20were%20developed%20to%20combat%20these%20problematic%20design,understand%2C%20maintain%2C%20and%20extend)

What is the usage of solid principles?

SOLID Principles is a coding standard that all developers should have a clear concept for developing software properly to avoid a bad design. It was promoted by Robert C Martin and is used across the object-oriented design spectrum. When applied properly it makes your code more extendable, logical, and easier to read.

(https://medium.com/mindorks/solid-principles-explained-with-examples-79d1ce114ace)

Single Responsibility

Here in this blog , i am going to discover about Single Responsibility Principle through in this four sections


1) Introduction
2) Usage 
3) Violations of Single Responsibility
4) Benefits

1) Introduction

"A class should have one and only one reason to change, meaning that a class should have only one job"

This is the statement that we can found in anywhere when you search it in google.

When we takes a computer program , it may contains many classes. So each class of the computer program must have responsibility over a single part of that program’s functionality,which should encapsulates.(Wikipedia)

That means, there may not contain classes that are not having any duty with the program. Every class in the program must contain a responsibility over the program.

What do you mean by responsibility?

According to Martin, responsibility means reason to change and concludes that a class or module should have one and only one reason to be changed (e.g. re written)- (Wikipedia)


2) Usage 

Let’s assume there is a program to generate a PDF file according to the information that are given by the user according to the given format by the program.

So in this scenario ,

Input       –       Details  given by the user according to the format that is provided by the program

Process   –       Generation PDF report

Output    –       PDF report including the details that are entered by the user

Note : There is a one class to generate the report according to a format.

So here the output can be changed according to several reasons,

-          If the user entered different details apart form the format that is given by the program

-          If user wants to send that PDF file to someone through the system apart from downloading it

So here, the thing is do not use the same class to do both changes that I mentioned above. It is a bad approach. Instead of using same class to do both changes we have to use two separate classes for each change or requirement. That’s the usage of Single Responsibility.


3) Example with Violations of Single Responsibility


There is a class called Student.

This example violates Single Responsibility.

Here, Student class is for represent the Student. So Register(),Apply() are responsibilities  of the Student.

So if we want to make changes to these Register or Apply responsibilities, it may be affect to the Student model either by adding an extra properties.

 So when apply the Single Responsibility we have to keep these methods in a separate class.

 


 



So if a class has more than one responsibility, they become coupled. Changes to one responsibility may restrict the class’s ability to meet other requirements.

4) Benefits

  • The class is easier to understand
  • The class is more reusable
  • The class is easier to maintain


Comments

Popular posts from this blog

Rest API

 What is REST? REST, or Representational State Transfer, is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other. REST-compliant systems, often called Restful systems, are characterized by how they are stateless and separate the concerns of client and server. (Wikipedia) Restful Web Services  is a lightweight, maintainable, and scalable service that is built on the REST architecture. Restful Web Service, expose API from your application in a secure, uniform, stateless manner to the calling client. The calling client can perform predefined operations using the Restful service. The underlying protocol for REST is HTTP. REST stands for Representational State Transfer Difference between REST API s and Database People think REST API are Databases. My main purpose is to solve the conflict between this REST API and databases because when REST API are used by the developers , they treat REST A...

MongoDB vs MySQL

  MongoDB and MySQL are two databases that are used by developers when implementing various enterprise applications. Let's compare and contrast these two databases according to the following topics What is MongoDB and MySQL? Differences Why Use ? Disadvantages When to Use? What is MongoDB and MySQL? MongoDB MySQL MongoDB is an open-source database developed by MongoDB, Inc. MongoDB stores data in JSON-like documents that can vary in structure. It is a popular NoSQL database. MySQL is a popular open-source database developed, distributed and supported by Oracle Corporation. It    relational database management system (RDBMS) that is developed, distributed and supported by Oracle Corporation Differences NoSQL vs SQL NoSQL databse              SQL database No need predefined structure Need predefined structure   Non-Relational Database Relational Database Data as of JSON documents Represents data table and...

Version Control

  In  software engineering ,  version control  is a class of systems responsible for managing changes to  computer programs , documents, large web sites, or other collections of information. Version control is a component of  software configuration management .  [Wikipedia] Other names       –       Revision control , Source control , Source code management Version Control Systems  They are software tools that help software teams manage changes to source code over time. Version control software is an essential part of the every-day of the modern software team's professional practices. Using version control systems, several developers, designers, and team members will work on the same project. These systems, also known as version control systems or VCS, ensure that everybody has access to the most current code. Why Version Control system is so Important?      Groups of software developers ar...