Roadmap › SOLID Principles with Examples › Module: L, I and D
Lessons in this course (8)
4. I — Interface Segregation Principle
Intermediate★ Must knowLesson 4 of 8
Clients should not be forced to depend on methods they don't use. Split fat interfaces into role interfaces.
csharp
// ❌ Fat interface — a basic printer must implement Fax and Scan
public interface IMachine { void Print(Doc d); void Scan(Doc d); void Fax(Doc d); }
public class BasicPrinter : IMachine
{
public void Print(Doc d) { }
public void Scan(Doc d) => throw new NotSupportedException(); // also an LSP smell
public void Fax(Doc d) => throw new NotSupportedException();
}
// ✅ Role interfaces
public interface IPrinter { void Print(Doc d); }
public interface IScanner { void Scan(Doc d); }
public interface IFax { void Fax(Doc d); }
public class BasicPrinter2 : IPrinter { public void Print(Doc d) { } }
public class OfficeMachine : IPrinter, IScanner, IFax { /* all three */ }Real-world .NET example: IReadOnlyList<T> vs IList<T>; IEnumerable<T> vs ICollection<T>.