Interface Segregation Principle (ISP)
Explanation:
A class should not be forced to implement interfaces it does not use.
Real-World Example:
A desktop computer might have a method to plug in a mouse, but a tablet shouldn't be forced to implement this method.
C# Implementation:
// Violating ISP public interface IWorker { void Work(); void Eat(); } public class Robot : IWorker { public void Work() { Console.WriteLine("Robot working"); } public void Eat() { throw new NotImplementedException("Robots don't eat"); } } // Following ISP public interface IWorkable { void Work(); } public interface IFeedable { void Eat(); } public class Human : IWorkable, IFeedable { public void Work() { Console.WriteLine("Human working"); } public void Eat() { Console.WriteLine("Human eating"); } } public class Robot : IWorkable { public void Work() { Console.WriteLine("Robot working"); } }
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: