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 perform lazy loading with generic method.

Introduction Let’s develop C# console application to perform lazy loading with generic method.    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 because we will create the console application and click on the OK button. We need to include the following references in our application. using  System;   using  System.Collections.Generic;   using  System.Linq;  In the main method, I have created 2 classes and 1 method EducationProfile<T> – To define class members. Candiate<T> = Initialize lazy loading in the class. CallLazyLoading = call the function Please refer to the code snippet below static   void  Main( string [] args)   {        try ...

How to perform Insert/Update/Delete operation in Generic Collections in C#

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 because we will create the console application and click on the OK button. we need to include the following references in our application. using System; using System.Collections.Generic; Insert, Update, Delete, Get perform in List collection. public static void DML_List() { // Creating a List of integers IList<int> list = new List<int>(); // Printing the insert opeartion Console.WriteLine("INSERT"); // Adding elements in list collection list.Add(100); list.Add(200); list.Add(300); list.Add(400); list.Add(400); // Printing the element from list collection foreach (object x in list) { Console.WriteLine("Result : " + x); } Console.WriteLine("\n"); // Printing th...