
Spis treści
Interface segregation principle – Zasada segregacji interfejsów
Wiele dedykowanych i małych interfejsów jest lepsze niż jeden ogólny.
Jak wykorzystywać w praktyce ISP?
Należy rozdzielać interfejsy na mniejsze i przyłożyć się do projektowania abstrakcyjnych elementów w aplikacji. Za granicę podziału najlepiej wybierać jest miejsce, które pozwala wydzielić obszary zgodne z zasadą pojedynczej odpowiedzialności. Każdy obszar powinien mieć jeden powód do zmiany. Powinien zmniejszać zależności i zwiększać spójność. Interfejsy powinny zawierać taki zbiór metod, aby klasy je implementujące nie musiały implementować metod, których nie chcą implementować.
Przykład
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(); }
Klasa TableImporter implementuje metodę, z której w ogóle nie korzysta. Lepiej jest oddzielić interfejs DataImporter na mniejsze interfejsy. Wtedy wszystkie klasy będą implementowały tylko to, co chcą.
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(); }
Wszystkie posty związane z mini projektem: Poznaj zasady SOLID i OOP:
Źródła
Obraz główny
Materiały
- Czysta architektura — 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/