Last active
January 13, 2026 21:26
-
-
Save sunmeat/7dc965f5a5ae77902e1ca29d54a63ebc to your computer and use it in GitHub Desktop.
ISP bad example C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface IBird | |
| { | |
| void Fly(); | |
| void Tweet(); | |
| void Eat(); | |
| } | |
| class Sparrow : IBird | |
| { | |
| // ... | |
| public void Fly() | |
| { | |
| Console.WriteLine("Горобець летить..."); | |
| } | |
| public void Tweet() | |
| { | |
| Console.WriteLine("Горобець цвірінькає..."); | |
| } | |
| public void Eat() | |
| { | |
| Console.WriteLine("Горобець їсть..."); | |
| } | |
| } | |
| class Duck : IBird | |
| { | |
| // ... | |
| public void Fly() | |
| { | |
| Console.WriteLine("Качка летить..."); | |
| } | |
| public void Tweet() | |
| { | |
| throw new NotSupportedException("Непідтримувана операція :("); | |
| } | |
| // і ми не можемо не реалізувати цей метод, | |
| // бо він є в вимогах інтерфейсу | |
| public void Eat() | |
| { | |
| Console.WriteLine("Качка їсть..."); | |
| } | |
| } | |
| class Penguin : IBird | |
| { | |
| // ... | |
| public void Fly() | |
| { | |
| throw new NotSupportedException("Непідтримувана операція :("); | |
| } | |
| public void Tweet() | |
| { | |
| throw new NotSupportedException("Непідтримувана операція :("); | |
| } | |
| public void Eat() | |
| { | |
| Console.WriteLine("Пінгвін їсть..."); | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = System.Text.Encoding.UTF8; | |
| var sparrow = new Sparrow(); | |
| sparrow.Tweet(); | |
| var duck = new Duck(); | |
| duck.Tweet(); // викличе виняток | |
| var penguin = new Penguin(); | |
| penguin.Fly(); // викличе виняток | |
| penguin.Tweet(); // викличе виняток | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment