-
Interface Segregation Principle
-
The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. It states that "clients should not be forced to implement the interfaces they do not use".
Understanding the Interface Segregation Principle (ISP)- The interface segregation principle is required to solve the application's design problem.
- When all the tasks are done by a single class or, in other words, one class is used in almost all the application classes, then it has become a fat class with lots of logic and functionalities written together.
- Inheriting such a class will result in sharing methods/functions that are not relevant to derived classes, but since they are present in the base class, they will be inherited in the derived class.
- Using interface segregation principle, we can create separate interfaces for each requirement instead of having a single class to do the same work.
In simpler terms, it advocates for creation of smaller and more specific interfaces rather than large, monolithic ones.
Examples of Interface Segregation Principle in C#
Bad Example: Violating ISP in C#// Interface segregation violated public interface IMachine { void Print(); void Scan(); void Fax(); } public class AllInOnePrinter : IMachine { public void Print() { Console.WriteLine("Printing..."); } public void Scan() { Console.WriteLine("Scanning..."); } public void Fax() { Console.WriteLine("Faxing..."); } }
- Violation: The IMachine interface combines unrelated functionalities (Print, Scan, and Fax).
- Impact: Clients like AllInOnePrinter implementing IMachine interface are forced to provide implementations for all methods, even if they do not need them. This violates ISP because clients (in this case - printers or scanners) should not be forced to depend on methods they don't use.
Let's consider an example where ISP is applied correctly:
// Interface segregation applied public interface IScanner { void Scan(); } public interface IPrinter { void Print(); } public class MultiFunctionDevice : IScanner, IPrinter { public void Scan() { Console.WriteLine("Scanning..."); } public void Print() { Console.WriteLine("Printing..."); } } public class OldFashionedPrinter : IPrinter { public void Print() { Console.WriteLine("Printing..."); } }
- Interfaces: IScanner and IPrinter are segregated into specific functionalities (Scan and Print respectively).
- Classes: MultiFunctionDevice implements both interfaces as it supports both scanning and printing.
- OldFashionedPrinter implements only IPrinter, adhering to ISP by only depending on the interface it needs.
Applying the Interface Segregation Principle (ISP) in C# leads to interfaces that are focused and cohesive, with each interface representing a single responsibility or role. This approach enhances code maintainability, flexibility, and scalability by reducing unnecessary dependencies and ensuring that classes only implement the interfaces they actually need.
In contrast, violating ISP results in bloated interfaces that can lead to maintenance challenges and coupling issues in the long term. Therefore, adhering to ISP helps in writing cleaner, more maintainable code in object-oriented systems.