Spis treści
Interface segregation principle
Many small and dedicated interfaces are better than few and general.
Hot to use ISP in practice?
You should separate the interfaces into smaller interfaces and better design of abstract elements in the application. The best place for a division is taking the Single Responsibility Principle. Each area should have one reason to change. It should reduce dependencies and increase consistency. Interfaces should contain such a set of methods that classes implementing them do not have to implement methods they do not want to implement.
Example
class Program { static void Main(string[] args) { IDataImporter reportImporter = new ReportImporter(); Console.WriteLine(reportImporter.GetDataFromPdf()); Console.WriteLine(reportImporter.GetDataFromExcel()); IDataImporter pictureImporter = new TableImporter(); Console.WriteLine(pictureImporter.GetDataFromPdf()); Console.WriteLine(pictureImporter.GetDataFromExcel()); } } internal class TableImporter : IDataImporter { public string GetDataFromExcel() { return "Table from Excel"; } public string GetDataFromPdf() { //We do not return a table from pdf file return string.Empty; } } internal class ReportImporter : IDataImporter { public string GetDataFromExcel() { return "Report from Excel"; } public string GetDataFromPdf() { return "Report from Pdf"; } } internal interface IDataImporter { string GetDataFromExcel(); string GetDataFromPdf(); }
Class TableImporter implements a method, which is not using. It is better, to separate interface DatImporter to smaller interfaces. Then all classes will implement what they need.
class Program { static void Main(string[] args) { var reportImporter = new ReportImporter(); Console.WriteLine(reportImporter.GetDataFromPdf()); Console.WriteLine(reportImporter.GetDataFromExcel()); IExcelParser pictureImporter = new TableImporter(); Console.WriteLine(pictureImporter.GetDataFromExcel()); } } internal class TableImporter : IExcelParser { public string GetDataFromExcel() { return "Table from Excel"; } } internal class ReportImporter : IExcelParser, IPdfParser { public string GetDataFromExcel() { return "Report from Excel"; } public string GetDataFromPdf() { return "Report from Pdf"; } } internal interface IExcelParser { string GetDataFromExcel(); } internal interface IPdfParser { string GetDataFromPdf(); }
All posts from mini project: Learn SOLID and OOP principles:
- SOLID
- SOLID exercises
- S like Single responsibility principle
- SOLID exercises — Kata
- O as Open-closed principle
- L jak Liskov Substitution Principle
- Interface segregation principle
- KISS — Keep it simple, stupid
- DRY — Don’t repeat yourself
- Dependency inversion principle
- SLAP — Single Level of Abstraction Principle
- Composition Over Inheritance
- Encapsulate what changes
- Lod — Law of Demeter
- ES as Exercises of Single responsibility principle
- EO as Exercises of Open/closed principle
- EL as Exercises of Liskov Substitution Principle
- EI as Eexrcises of Interface segregation principle
- ES as Exercises of Dependency Inversion Principle
- Object-oriented programming
- OOP — Object-Oriented Programming — Advice
- OOP — Object Oriented Programming
Sources
Main image
Materials
- Clean Architecture — Robert C. Martin
- https://code.tutsplus.com/tutorials/solid-part-3-liskov-substitution-interface-segregation-principles–net-36710
- https://code.tutsplus.com/tutorials/solid-part-4-the-dependency-inversion-principle–net-36872
- https://lukaszkosiorowski.pl/programowanie/solid-zasada-segregacji-interfejsow/
- http://tomaszjarzynski.pl/solid-czesc-4-zasada-segregacji-interfejsow/
- http://devman.pl/pl/techniki/zasady-solid-4-zasada-segregacji-interfejsowinterface-segregation/
- https://www.modestprogrammer.pl/solid-interface-segregation-principle-isp-wszystko-co-powinienes-wiedziec-o-zasadzie-segregacji-interfejsow
- https://www.ifcode.pl/php-solid-i-zasada-segregacji-interfejsow/
Sorry, there was a YouTube error.