SRP principle

 The SRP is one of the five SOLID principles of object-oriented design, which are a set of guidelines for creating software that is flexible, maintainable, and scalable. The SRP states that a class should have only one reason to change, or in other words, should have only one responsibility. This means that the class should do one thing and do it well.

The benefits of adhering to the SRP include:

  1. Improved code readability: By having a single responsibility, a class becomes easier to read and understand. Developers can quickly identify what the class does and how it fits within the application.
  2. Easier maintenance: A class with a single responsibility is easier to maintain because changes to the class are limited to that responsibility. This makes it easier to modify the class without affecting other parts of the application.
  3. Increased flexibility: A class with a single responsibility is more flexible because it can be reused in different contexts. This makes it easier to compose classes and build more complex systems.
  4. Better testability: A class with a single responsibility is easier to test because the tests can focus on the specific responsibility of the class. This makes it easier to write unit tests that cover all of the class's functionality.
 Example without the Single Responsibility Principle (SRP)

// Example without the Single Responsibility Principle (SRP)

class Calculator {
    func add(_ a: Int, _ b: Int) -> Int {
        return a + b
    }
    
    func subtract(_ a: Int, _ b: Int) -> Int {
        return a - b
    }
    
    func multiply(_ a: Int, _ b: Int) -> Int {
        return a * b
    }
    
    func divide(_ a: Int, _ b: Int) -> Int {
        return a / b
    }
    
    func calculate(_ a: Int, _ b: Int, operator: String) -> Int {
        switch `operator` {
        case "+":
            return add(a, b)
        case "-":
            return subtract(a, b)
        case "*":
            return multiply(a, b)
        case "/":
            return divide(a, b)
        default:
            fatalError("Invalid operator")
        }
    }
}

 Example with the Single Responsibility Principle (SRP)

class Addition {
    func add(_ a: Int, _ b: Int) -> Int {
        return a + b
    }
}

class Subtraction {
    func subtract(_ a: Int, _ b: Int) -> Int {
        return a - b
    }
}

class Multiplication {
    func multiply(_ a: Int, _ b: Int) -> Int {
        return a * b
    }
}

class Division {
    func divide(_ a: Int, _ b: Int) -> Int {
        return a / b
    }
}

class Calculator {
    let addition = Addition()
    let subtraction = Subtraction()
    let multiplication = Multiplication()
    let division = Division()
    
    func calculate(_ a: Int, _ b: Int, operator: String) -> Int {
        switch `operator` {
        case "+":
            return addition.add(a, b)
        case "-":
            return subtraction.subtract(a, b)
        case "*":
            return multiplication.multiply(a, b)
        case "/":
            return division.divide(a, b)
        default:
            fatalError("Invalid operator")
        }
    }
}

Comments

Popular Posts