Logo

dev-resources.site

for different kinds of informations.

Single Responsibility Principle (SRP)

Published at
12/5/2024
Categories
solidprinciples
Author
mofshamim
Categories
1 categories in total
solidprinciples
open
Author
9 person written this
mofshamim
open
Single Responsibility Principle (SRP)

Each class should have only one responsibility or reason to change.

This means each class should focus on a single functionality or responsibility and not combine unrelated tasks.

Why is SRP Important?

  • Simplifies Maintenance : Changes to a specific responsibility are isolated, reducing the risk of impacting unrelated code.

  • Improves Code Readability : Classes are smaller and easier to understand.

  • Enhances Reusability : Class can be reused in different contexts without carrying unnecessary responsibilities.

  • Eases Testing : Testing a single responsibility is straightforward, as the class has fewer dependencies.

  • Reduces Coupling : Avoids tightly coupling unrelated functionalities, making the codebase more flexible.

How to Apply SRP

Here’s an example of implementing SRP for a ticket booking scenario. The solution demonstrates how to handle booking logic, database persistence, email notifications, and error logging using separate classes.

Classes Overview
BookingService: Core business logic for booking tickets.
DatabaseService: Handles database interactions.
EmailService: Sends email notifications.
LoggerService: Logs errors or other information.

Code Implementation

  • LoggerService Handles logging operations.
public class LoggerService
{
    public void LogError(string message)
    {
        // Log error to a file or monitoring system
        Console.WriteLine($"Error: {message}");
    }

    public void LogInfo(string message)
    {
        // Log general information
        Console.WriteLine($"Info: {message}");
    }
}
Enter fullscreen mode Exit fullscreen mode
  • EmailService Handles sending emails.
public class EmailService
{
    public void SendEmail(string to, string subject, string body)
    {
        // Simulated email sending logic
        Console.WriteLine($"Email sent to {to} with subject: {subject}");
    }
}
Enter fullscreen mode Exit fullscreen mode
  • DatabaseService Handles database operations.
public class DatabaseService
{
    public void SaveBooking(string bookingDetails)
    {
        // Simulated database save logic
        Console.WriteLine($"Booking saved: {bookingDetails}");
    }
}
Enter fullscreen mode Exit fullscreen mode
  • BookingService Core business logic for ticket booking. It orchestrates the other services.
public class BookingService
{
    private readonly DatabaseService _databaseService;
    private readonly EmailService _emailService;
    private readonly LoggerService _loggerService;

    public BookingService(DatabaseService databaseService, EmailService emailService, LoggerService loggerService)
    {
        _databaseService = databaseService;
        _emailService = emailService;
        _loggerService = loggerService;
    }

    public void BookTicket(string userEmail, string bookingDetails)
    {
        try
        {
            // Step 1: Business logic for booking (e.g., validation, seat availability)
            _loggerService.LogInfo("Booking process started.");

            // Step 2: Save booking to the database
            _databaseService.SaveBooking(bookingDetails);

            // Step 3: Send confirmation email
            _emailService.SendEmail(userEmail, "Booking Confirmation", "Your booking is confirmed!");

            _loggerService.LogInfo("Booking process completed successfully.");
        }
        catch (Exception ex)
        {
            // Step 4: Log any errors
            _loggerService.LogError($"Error during booking: {ex.Message}");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Program Execution Tie everything together and simulate the booking process.
public class Program
{
    public static void Main(string[] args)
    {
        // Dependency setup
        var databaseService = new DatabaseService();
        var emailService = new EmailService();
        var loggerService = new LoggerService();

        var bookingService = new BookingService(databaseService, emailService, loggerService);

        // Simulated booking details
        string userEmail = "[email protected]";
        string bookingDetails = "Flight: XYZ123, Date: 2024-12-01, Seat: 12A";

        // Book the ticket
        bookingService.BookTicket(userEmail, bookingDetails);
    }
}
Enter fullscreen mode Exit fullscreen mode

Execution Flow

  • Business Logic Execution: Booking process begins with validation and checks.
  • Database Save: The booking details are stored using DatabaseService.
  • Email Notification: A confirmation email is sent using - EmailService.
  • Error Handling: Any exceptions during the process are logged by LoggerService.

This approach ensures a clean, modular design while adhering to SOLID principles.

solidprinciples Article's
30 articles in total
Favicon
ISP - O Princípio da Segregação de Interface
Favicon
Disadvantages of the Single Responsibility Principle(SRP)
Favicon
Guia: O que é SOLID
Favicon
Interface Segregation Principle (ISP)
Favicon
Dependency Inversion Principle (DIP)
Favicon
Liskov Substitution Principle (LSP)
Favicon
Create your own Logger using Open-closed principle
Favicon
SOLID: Dependency Inversion Principle (DIP) in C#
Favicon
SOLID: Principio de Abierto/Cerrado
Favicon
LSP - O Princípio da Substituição de Liskov
Favicon
SOLID: Principio de Responsabilidad Única
Favicon
SOLID Principles for React / React Native Development
Favicon
Single Responsibility Principle (SRP)
Favicon
Solid Prensipleri
Favicon
Mastering SOLID Principles in .NET Core: A Path to Clean and Scalable Code
Favicon
OCP - O Princípio Aberto/Fechado
Favicon
SRP - O Princípio da Responsabilidade Única
Favicon
SOLID Design Principles
Favicon
Rethinking interfaces in Flutter projects
Favicon
Understanding the SOLID Principles in PHP and How They Improve Code Quality
Favicon
SOLID: The Liskov Substitution Principle (LSP) in C#
Favicon
Is Clean Code really practical?
Favicon
Open/Closed Principle (OCP)
Favicon
Single Responsibility Principle (SRP)
Favicon
Solid Principle in Simple English
Favicon
Why Clean Architecture and SOLID Principles Should Be the Foundation of Your Next Project!
Favicon
Seja um desenvolvedor melhor com S.O.L.I.D.
Favicon
SOLID: Open-Closed Principle (OCP) in C#
Favicon
SOLID Principles Explained in a Simple Way 🛠️✨ with Real-Life Examples
Favicon
Object Oriented Design Balance With Understanding Anti-Single Responsibility Principle

Featured ones: