{"id":2777,"date":"2023-07-16T08:24:00","date_gmt":"2023-07-16T06:24:00","guid":{"rendered":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2777"},"modified":"2023-07-18T10:34:33","modified_gmt":"2023-07-18T08:34:33","slug":"interface-segregation-principle","status":"publish","type":"post","link":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2777","title":{"rendered":"Interface Segregation Principle"},"content":{"rendered":"<p>Third principle states, that it is better to create some smaller interfaces, than one bigger &#8211; by doing that, we are getting rid of issues, where classes do not need method implementation of one big interface. Lets have a look at code below:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic interface IRaportable\n{\n    void PrintPdf();\n    void PrintExcel();\n}\n\npublic class SalaryRaport : IRaportable\n{\n    public void PrintPdf()\n    {\n        \/\/ print PDF file logic\n    }\n\n    public void PrintExcel()\n    {\n        \/\/ print CSV file logic\n    }\n}\n\npublic class HighSchoolExam : IRaportable\n{\n    public void PrintPdf()\n    {\n        \/\/ printing PDF\n    }\n\n    public void PrintExcel()\n    {\n        throw new NotImplementedException();\n    }\n}\n\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">We have created one <span style=\"background-color: #efefef;\">Interface<\/span>, which has two methods implemented. It would be fair to say, that it looks good &#8211; but it does not. Interface is although inherited by both of created classes, but each class does not implement a method as it should be. Here comes the question &#8211; why is that so crucial..? Lets assume that we have created a based typed <span style=\"background-color: #efefef;\">List<\/span>, through which we have to somehow iterate &#8211; in some cases there will be thrown an exception because of one thing &#8211; one class does not implement inherited method of an interface. In most cases in C# language, <span style=\"background-color: #efefef;\">Interfaces<\/span> implement max two methods, which beautifully gives an example of <em>Interface Segregation Principle.<\/em><\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ split one interface to two smaller ones\npublic interface IPrintablePdf\n{\n    void PrintPdf();\n}\n\npublic interface IPrintableExcel\n{\n    void PrintExcel();\n}\n\/\/ SalaryRaport inheritance from two interfaces\npublic class SalaryRaport : IPrintablePdf, IPrintableExcel\n{\n    public void PrintPdf()\n    {\n        \/\/ print PDF file logic\n    }\n    \n    public void PrintExcel()\n    {\n        \/\/ print CSV file logic\n    }\n}\n\npublic class HighSchoolExam : IPrintablePdf\n{\n    public void PrintPdf()\n    {\n        \/\/ printing PDF\n    }\n}\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Simple principle containing concise and small classes.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-2777","post","type-post","status-publish","format-standard","hentry","category-solid-articles"],"_links":{"self":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2777"}],"version-history":[{"count":9,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2777\/revisions"}],"predecessor-version":[{"id":2833,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2777\/revisions\/2833"}],"wp:attachment":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}