#4 Interface Segregation Principle [‘I’ in SOLID]

ISP – Interface Segregation Principle

The Interface Segregation Principle is the fourth principle in the Solid Design Principles.

Clients should not be forced to depend on interfaces they do not use.

Violating ISP:

protocol Worker {
func ea…


This content originally appeared on DEV Community and was authored by vinaykumar0339

ISP - Interface Segregation Principle

The Interface Segregation Principle is the fourth principle in the Solid Design Principles.

  1. Clients should not be forced to depend on interfaces they do not use.

Violating ISP:

protocol Worker {
    func eat()
    func work()
}

class HumanWorker: Worker {
    func eat() {
        print("Human is eating...")
    }

    func work() {
        print("Human is working...")
    }
}

class RobotWorker: Worker {
    func work() {
        print("Robot is working...")
    }

    func eat() {
        fatalError("Robot can't eat...")
    }
}

// usage
let robot = RobotWorker()
robot.eat()  // This will cause a runtime error

Issues with Violating ISP:

  1. Unnecessary Methods:
    • Classes are forced to implement methods they do not use, leading to potential runtime errors.
  2. Increased Complexity:
    • Interfaces become bloated with methods irrelevant to some implementations.
  3. Reduced Maintainability:
    • Changes to interfaces affect all implementing classes, even if they do not use the changed methods.

Adhering to ISP
To adhere to ISP, separate the protocol into small pieces:

protocol Workable {
    func work()
}

protocol Eatable {
    func eat()
}

protocol WorkerISP: Workable, Eatable {}

class HumanWorkerISP: WorkerISP {
    func eat() {
        print("HumanWorkerISP is eating...")
    }

    func work() {
        print("HumanWorkerISP is working...")
    }
}

class RobotWorkerISP: Workable {
    func work() {
        print("RobotWorkerISP is eating...")
    }
}

// usage
let humanWorker = HumanWorkerISP()
humanWorker.eat()
humanWorker.work()

let robotWorker = RobotWorkerISP()
robotWorker.work()

Benefits of Adhering to ISP:

  1. Improved Maintainability:
    • Smaller, specific interfaces are easier to understand and maintain.
  2. Enhanced Flexibility:
    • Changes to an interface do not affect clients that do not use that part of the interface.
  3. Increased Reusability:
    • Specific interfaces can be reused more easily across different parts of the application.

Drawbacks of Adhering to ISP:

  1. More Interfaces:
    • Can lead to an increase in the number of interfaces, making the codebase larger.
  2. Initial Complexity:
    • Designing multiple specific interfaces may require more detailed planning and initial effort.

Mitigating Drawbacks:

  1. Balanced Approach:
    • Apply ISP judiciously, balancing between simplicity and the need for specificity.
  2. Clear Documentation:
    • Maintain clear and concise documentation to help developers understand the purpose of each interface.
  3. Use of Design Patterns:
    • Employ design patterns that naturally adhere to ISP, such as the Adapter pattern, to manage complexity.

Conclusion:
By thoughtfully understanding and applying the Interface Segregation Principle, you can create more maintainable, understandable, and flexible software. Ensuring that clients only depend on the interfaces they use promotes better software design and enhances the overall quality of the codebase.

Liskov Substitution Principle
Check My GitHub Swift Playground Repo.


This content originally appeared on DEV Community and was authored by vinaykumar0339


Print Share Comment Cite Upload Translate Updates
APA

vinaykumar0339 | Sciencx (2024-06-30T18:15:55+00:00) #4 Interface Segregation Principle [‘I’ in SOLID]. Retrieved from https://www.scien.cx/2024/06/30/4-interface-segregation-principle-i-in-solid/

MLA
" » #4 Interface Segregation Principle [‘I’ in SOLID]." vinaykumar0339 | Sciencx - Sunday June 30, 2024, https://www.scien.cx/2024/06/30/4-interface-segregation-principle-i-in-solid/
HARVARD
vinaykumar0339 | Sciencx Sunday June 30, 2024 » #4 Interface Segregation Principle [‘I’ in SOLID]., viewed ,<https://www.scien.cx/2024/06/30/4-interface-segregation-principle-i-in-solid/>
VANCOUVER
vinaykumar0339 | Sciencx - » #4 Interface Segregation Principle [‘I’ in SOLID]. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/30/4-interface-segregation-principle-i-in-solid/
CHICAGO
" » #4 Interface Segregation Principle [‘I’ in SOLID]." vinaykumar0339 | Sciencx - Accessed . https://www.scien.cx/2024/06/30/4-interface-segregation-principle-i-in-solid/
IEEE
" » #4 Interface Segregation Principle [‘I’ in SOLID]." vinaykumar0339 | Sciencx [Online]. Available: https://www.scien.cx/2024/06/30/4-interface-segregation-principle-i-in-solid/. [Accessed: ]
rf:citation
» #4 Interface Segregation Principle [‘I’ in SOLID] | vinaykumar0339 | Sciencx | https://www.scien.cx/2024/06/30/4-interface-segregation-principle-i-in-solid/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.