Skip to main content

Single Responsibility Principle

 

Single Responsibility Principle

We will be discussing the Single Responsibility Principle also known as SRP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software.

This principle said that each class should have one responsibility, one single purpose. This means that a class will do only one job, which leads us to conclude it should have only one reason to change.

Example

Let us define a class that contains code that changes the text and printing the text in some way.

Initialize the class (StringManipulating.cs) in this class there are two methods one is AppendString () second is PrintString () in this case SRP principle is not to work because of two responsibilities in one class.

public class StringManipulating {
    private string text;
    public StringManipulating(string text) {
        this.text = text;
    }
    public string GetString() {
        return text;
    }
    public void AppendString(string newText) {
        text = (string) text.Concat(newText);
    }
    public void PrintText() {
        Console.WriteLine(value: GetString());
    }
  }
}
C#

Again, Modify the Code

Initialize the class (Printing.cs) and move the printText Method to this class.

public class Printing {
    readonly StringManipulating StringManipulating = new StringManipulating("Nirmal Dayal");
    public void PrintText() {
        Console.WriteLine(value: StringManipulating.GetString());
    }
}
C#

Conclusion

Even though the name of the principle is self-explanatory, we can see how easy it is to implement incorrectly. Make sure to distinguish the responsibility of every class.

Comments

Popular posts from this blog

How to find the count to convert a pyramid into a square using C#.net

Introduction   Let’s develop a simple C# console application to find the count of convert a pyramid into a square. Getting Started In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project. Now we will select the ASP.NET 5 Console Application to create the console application, click on the OK button. Include the following references in the application: using  System;   using  System.Collections.Generic;   using  System.Linq;  In the main method, I have created 2 methods 1.PrintPyramid – To print the triangle in given input. 2.ConvertPyramidToSquare = Convert triangle into square. Please refer the code snippet below: static   void  Main( string [] args)   {   try    {   PrintPyramid();   ConvertPyram...

Open Closed Principle

  Open Closed Principle We will be discussing the Open-Closed Principle also known as OCP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software. This principle said that should be open for extension but closed for modification. We should write a code that does not require modification every time a customer changes its request. Example Let us define a class that contains salary calculations. Let us imagine that we have a task where we need to calculate the total cost of all the developer salaries in a single company. To get started, we are going to create the model class first, public class EmployeeReport { public int Id { get ; set ; } public string Name { get ; set ; } public string Level { get ; set ; } public int WorkingHours { get ; set ; } public double HourlyRate { get ; ...

Liskov Substitution Principle

Liskov Substitution Principle We will be discussing the Liskov Substitution Principle also known as LSP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software. This principle said that Subtypes must be substitutable for their base types. In other words, if we substitute a superclass object reference with an object of any of its subclasses, the program should not break. Example Here we design three classes for employees and one abstract class and Implement abstract class on these classes, PermamentEmployee.cs ContractEmployee.cs TemporaryEmployee.cs class LSP { public LSP ( ) { Run ( ) ; } public void Run ( ) { Console . ReadLine ( ) ; } public abstract class Employee { protected string FullName { get ; set ; } protected abstract string CalculateBonus ( ) ; } public class PermamentEmployee : Employee ...