{"id":499,"date":"2021-01-03T09:34:46","date_gmt":"2021-01-03T04:04:46","guid":{"rendered":"https:\/\/saurabhjain1537.wordpress.com\/?p=499"},"modified":"2025-02-27T07:46:42","modified_gmt":"2025-02-27T07:46:42","slug":"solid-principles-interface-segregation","status":"publish","type":"post","link":"https:\/\/saurabhjain.dev\/?p=499","title":{"rendered":"SOLID Principles: Interface Segregation"},"content":{"rendered":"\n<p>Interface segregation is another technique that helps in keeping the system loosely coupled by dividing the functionality into small components.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>The Interface Segregation Principle &#8220;I&#8221;<\/strong><\/p>\n\n\n\n<p>It states that no client should be forced to depend on methods it does not use. In other words, only relevant methods and functionalities should be exposed to clients and clients should have ability to pick and choose what&#8217;s relevant to them.<\/p>\n\n\n\n<p>The problem with creating large Interfaces and hence heavy classes is that every client may not require all the functionality offered by the large Interface but they end up including things they don&#8217;t use. This leads to a very common issue in the Software Life-cycle where things break as a side-effect of some other change. Unexpected errors may occur for a client when there are changes in certain areas of the underlying application (which are not even being used by the client).<\/p>\n\n\n\n<p>Interface Segregation can be considered an extension of Single Responsibility Principle which helps in segregating related but different functionalities. <\/p>\n\n\n\n<p>Consider the example of a generalized E-Commerce platform which can be utilized for sale &amp; delivery of physical goods and electronic subscription. <\/p>\n\n\n\n<p>Classes which handle Delivery can be defined like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2020\/12\/04-isp-01-delivery.png\"><img loading=\"lazy\" decoding=\"async\" width=\"355\" height=\"255\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2020\/12\/04-isp-01-delivery.png?w=355\" alt=\"\" class=\"wp-image-510\" \/><\/a><figcaption>Figure 1<\/figcaption><\/figure><\/div>\n\n\n\n<p> <\/p>\n\n\n\n<p>Some of <strong>OrderDelivery <\/strong>methods like <em>assignPostalServiceProvider()<\/em> may not be applicable for ElectronicDelivery. Clients that only require <strong>ElectronicDelivery<\/strong> will unnecessarily get the APIs specific to Physical Delivery. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ElectronicDelivery implements OrderDelivery {\n\n  @Override\n  public void assignPostalServiceProvider(String provider) {\n    \/\/Do nothing\n  }\n}<\/code><\/pre>\n\n\n\n<p>Any changes related to Physical Delivery in the <strong>OrderDelivery<\/strong> interface, would require updates in the <strong>ElectronicDelivery<\/strong> concrete implementation too. It is useless but necessary change that can be avoided with proper segregation of Interfaces like:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2021\/01\/04-isp-02-delivery.png\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"255\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2021\/01\/04-isp-02-delivery.png?w=619\" alt=\"\" class=\"wp-image-513\" \/><\/a><figcaption>Figure 2<\/figcaption><\/figure><\/div>\n\n\n\n<p>The way classes are defined in Figure 2 is much cleaner and each class contains only relevant behavior.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>When each class contains only relevant functionality, then client applications are never impacted by unused dependencies. Interface Segregation helps in reducing side-effect bugs during the maintenance of Application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interface segregation is another technique that helps in keeping the system loosely coupled by dividing the functionality into small components. The Interface Segregation Principle &#8220;I&#8221; It states that no client should be forced to depend on methods it does not use. In other words, only relevant methods and functionalities should be exposed to clients and &hellip; <a href=\"https:\/\/saurabhjain.dev\/?p=499\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SOLID Principles: Interface Segregation<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[34,41,42,55],"class_list":["post-499","post","type-post","status-publish","format-standard","hentry","category-design","tag-interface-segregation","tag-object-oriented-design","tag-object-oriented-programming","tag-solid-principles"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=499"}],"version-history":[{"count":1,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":779,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions\/779"}],"wp:attachment":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}