CI jak Ćwiczenia Interface segregation principle, czyli Zasady segregacji interfejsów

przez Karol Bocian | 11 lutego, 2020

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

Kategoria: IT Projekt Poznaj zasady SOLID i OOP Tagi: it, OOP, programowanie, SOLID

O Karol Bocian

Programowanie i świat agentów programowalnych, który poznał na Wydziale Matematyki i Nauk Informacyjnych, wciągnął go w przemysł IT. W trakcie swojej praktyki zawodowej Karol zrozumiał, że nie ważne co się robi i kim się jest, ale wiedza z zarządzania przydaje się wszędzie. Rozpoczął studia na kierunku Zarządzanie i Inżyniera Produkcji. W przypadku Karola zarządzanie to nie tylko teoria czy praca, ale prawie każdy element jego życia, to jego pasja.