{"id":1661,"date":"2021-03-06T11:57:43","date_gmt":"2021-03-06T10:57:43","guid":{"rendered":"http:\/\/mateuszglowinski.pl\/?p=1661"},"modified":"2021-03-06T15:17:46","modified_gmt":"2021-03-06T14:17:46","slug":"interface-segregation-principle","status":"publish","type":"post","link":"https:\/\/mateuszglowinski.pl\/index.php\/2021\/03\/06\/interface-segregation-principle\/","title":{"rendered":"Interface Segregation Principle"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1661\" class=\"elementor elementor-1661\" data-elementor-settings=\"[]\">\n\t\t\t\t\t\t<div class=\"elementor-inner\">\n\t\t\t\t\t\t\t<div class=\"elementor-section-wrap\">\n\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-975d629 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"975d629\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-25e67e8\" data-id=\"25e67e8\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-d6e683f elementor-widget elementor-widget-text-editor\" data-id=\"d6e683f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\"><h4>What is &#8222;Interface Segregation Principle&#8221; (ISP) ?<\/h4><p>It is the <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">fourth<\/span><\/span> rule of the SOLID design rules (it stands for I in SOLID <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">acronym).<\/span><\/span><\/p><h4>Definition<\/h4><blockquote><p>Clients should not be forced to depend upon interfaces that they do no use.<\/p><div role=\"heading\" data-attrid=\"wa:\/description\" aria-level=\"3\" data-hveid=\"CAoQAA\">\u00a0<\/div><div lang=\"pl-PL\" data-md=\"61\"><div style=\"padding-left: 560px;\" role=\"heading\" data-attrid=\"wa:\/description\" aria-level=\"3\" data-hveid=\"CAoQAA\">Robert C. Martin (Uncle Bob)<\/div><\/div><\/blockquote><p><span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"1\"><span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">In simpler words<\/span><\/span>, we should avoid situation where class inheriting from interface do not use some parts of the interface. <\/span><\/span><span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"1\"><span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">Interfaces should be specific and as small as possible.<\/span><\/span> <\/span><\/span><\/p><h4>Real-life example<\/h4><p>Let&#8217;s say we want to implement classes for car, airplane and multifunctional car (like in Taxi movie).<\/p><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public interface Vehicle {\n    void drive();\n\n    void fly();\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Car implements Vehicle {\n    public void drive() {\n        System.out.println(\"Driving a car\");\n    }\n\n    public void fly() {\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Airplane implements Vehicle {\n    public void drive() {\n    }\n\n    public void fly() {\n        System.out.println(\"Flying a plane\");\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class MultiFunctionalCar implements Vehicle {\n    public void drive() {\n        System.out.println(\"Drive a multifunctional car\");\n    }\n\n    public void fly() {\n        System.out.println(\"Fly a multifunctional car\");\n    }\n}\n<\/pre><p>UML diagram of our application:<\/p><p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1696\" src=\"http:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-no-ISP.png\" alt=\"\" width=\"711\" height=\"391\" srcset=\"https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-no-ISP.png 711w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-no-ISP-300x165.png 300w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-no-ISP-160x88.png 160w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-no-ISP-680x374.png 680w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/p><p><em>Fly<\/em> method in <em>Car<\/em> class is empty, the same about <em>drive<\/em> method in <em>Airplane<\/em> class. It means that <em>Vehicle<\/em> interface contains too many parts for our needs and due to that our program does not meet <em>ISP<\/em> rule.<\/p><p>To fix it, we need to split <em>Vehicle<\/em> interface into smalled interfaces e.g. ICar and IAirplance.<\/p><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public interface ICar {\n    void drive();\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public interface IAirplane {\n    void fly();\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Car implements ICar {\n    public void drive() {\n        System.out.println(\"Driving a car\");\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Airplane implements IAirplane {\n    public void fly() {\n        System.out.println(\"Flying a plane\");\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class MultiFunctionalCar implements ICar, IAirplane {\n    public void drive() {\n        System.out.println(\"Drive a multifunctional car\");\n    }\n\n    public void fly() {\n        System.out.println(\"Fly a multifunctional car\");\n    }\n}\n<\/pre><p>Now classes implement only necessary methods, it means that interfaces are <span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">consistent with the <em>ISP<\/em> rule.<\/span><\/span><\/p><p>UML diagram of our application after refactoring:<\/p><p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1697\" src=\"http:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-with-ISP.png\" alt=\"\" width=\"711\" height=\"392\" srcset=\"https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-with-ISP.png 711w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-with-ISP-300x165.png 300w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-with-ISP-160x88.png 160w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vehicles-with-ISP-680x375.png 680w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/p><h4>Why should I follow &#8222;Interface Segregation Principle&#8221; ?<\/h4><p>Thanks to this rule, the code is easier to extend and maintain. Less tricky interfaces also mean more clear code.<\/p><h4>What to look for?<\/h4><p><em>ISP<\/em> is not the same as <em>LSP<\/em>. Focus in <em>ISP<\/em> is not about making classes replaceable, but rather on rethinking initial design and picking up proper abstractions when designing a system.<\/p><\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>What is &#8222;Interface Segregation Principle&#8221; (ISP) ? It is the fourth rule of the SOLID design rules (it stands for I in SOLID acronym). Definition&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34],"tags":[35],"_links":{"self":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1661"}],"collection":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1661"}],"version-history":[{"count":46,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1661\/revisions"}],"predecessor-version":[{"id":1752,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1661\/revisions\/1752"}],"wp:attachment":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}