{"id":3876,"date":"2017-11-22T13:31:07","date_gmt":"2017-11-22T08:01:07","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=3876"},"modified":"2017-11-22T13:43:15","modified_gmt":"2017-11-22T08:13:15","slug":"solid-interface-segregation-principle","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/","title":{"rendered":"SOLID: Interface Segregation Principle"},"content":{"rendered":"<h3>Interface Segregation Principle<\/h3>\n<p><strong>Previous Principle:\u00a0<a href=\"https:\/\/www.innovationm.com\/blog\/solid-liskovs-substitution-principle\/\" target=\"_blank\" rel=\"noopener\">Liskov&#8217;s Substitution Principle<\/a><\/strong><\/p>\n<p><em><code>A client should never be forced to implement an interface that it doesn\u2019t use or clients shouldn\u2019t be forced to depend on methods they do not use.<\/code><\/em><\/p>\n<p>In OOD we provide abstraction to module using interfaces, so we create an interface for a module and an implementation class. But now if we have to create an another module which only have some of the functionality as of previous module then we are forced to implement the whole interface and add some dummy implementation or throw Exception. This is known as\u00a0<code>fat interface<\/code>.<\/p>\n<p>So the Interface Segregation Principle states that in place of using one fat interface we create multiple small interfaces for submodules.<\/p>\n<p>Let&#8217;s take an example of a printer which can print, fax and scan. So we have a system like this:<\/p>\n<pre class=\"lang:java decode:true\">interface ISmartPrinter {\r\n    void print();\r\n    void fax();\r\n    void scan();\r\n}\r\n\r\nclass SmartPrinter implements ISmartPrinter {\r\n\r\n    public void print() {\r\n         \/\/ Printing code.\r\n    }\r\n    \r\n    public void fax() {\r\n         \/\/ Fax Code.\r\n    }\r\n    \r\n    public void scan() {\r\n         \/\/ Scanning code.\r\n    }\r\n}<\/pre>\n<p>Now suppose we also have a not so good printer which can only print, so we&#8217;re forced to implement the whole interface like:<\/p>\n<pre class=\"lang:java decode:true\">class EconomicPrinter implements ISmartPrinter {\r\n    public void print() {\r\n        \/\/Yes I can print.\r\n    }\r\n    \r\n    public void fax() {\r\n        throw new NotSupportedException();\r\n    }\r\n    \r\n    public void scan() {\r\n        throw new NotSupportedException();\r\n    }\r\n}<\/pre>\n<p><b>This is not good.<\/b><\/p>\n<p>So what we can do is apply ISP and divide the fat interface\u00a0<code>ISmartPrinter<\/code>\u00a0into three smaller interfaces,\u00a0<code>IPrinter<\/code>,\u00a0<code>IFax<\/code>\u00a0and\u00a0<code>IScanner<\/code>\u00a0like this:<\/p>\n<pre class=\"lang:java decode:true\">interface IPrinter {\r\n    void print();\r\n}\r\n\r\ninterface IFax {\r\n    void fax();\r\n}\r\n\r\ninterface IScanner {\r\n    void scan();\r\n}\r\n\r\n\r\nclass SmartPrinter implements IPrinter, IFax, IScanner {\r\n\r\n    public void print() {\r\n         \/\/ Printing code.\r\n    }\r\n    \r\n    public void fax() {\r\n         \/\/ Fax Code.\r\n    }\r\n    \r\n    public void scan() {\r\n         \/\/ Scanning code.\r\n    }\r\n}\r\n\r\n\r\nclass EconomicPrinter implements IPrinter {\r\n    public void print() {\r\n        \/\/Yes I can print.\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Keep in mind that using ISP can result in many small interfaces in code, but it will reduce the complexity, increase testability and also improve code quality. Smaller interfaces are easier to implement, improving flexibility and the possibility of reuse. But ISP can also be used based on experience, like identifying the areas which are more likely to have an extension in future.<\/p>\n<p><strong>Next:\u00a0<a href=\"https:\/\/www.innovationm.com\/blog\/solid-dependency-inversion-principle\/\" target=\"_blank\" rel=\"noopener\">Dependency Inversion Principle<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interface Segregation Principle Previous Principle:\u00a0Liskov&#8217;s Substitution Principle A client should never be forced to implement an interface that it doesn\u2019t use or clients shouldn\u2019t be forced to depend on methods they do not use. In OOD we provide abstraction to module using interfaces, so we create an interface for a module and an implementation class. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3889,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,71,8],"tags":[215,165,13],"class_list":["post-3876","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-ios","category-mobile","category-mobile-architecture-and-design","tag-design-pattern","tag-mobile","tag-mobile-design"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SOLID: Interface Segregation Principle - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SOLID: Interface Segregation Principle - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Interface Segregation Principle Previous Principle:\u00a0Liskov&#8217;s Substitution Principle A client should never be forced to implement an interface that it doesn\u2019t use or clients shouldn\u2019t be forced to depend on methods they do not use. In OOD we provide abstraction to module using interfaces, so we create an interface for a module and an implementation class. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-22T08:01:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-22T08:13:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/11\/InterfaceSegregationPrinciple.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"SOLID: Interface Segregation Principle\",\"datePublished\":\"2017-11-22T08:01:07+00:00\",\"dateModified\":\"2017-11-22T08:13:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/\"},\"wordCount\":234,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/InterfaceSegregationPrinciple.jpg\",\"keywords\":[\"Design Pattern\",\"Mobile\",\"Mobile Design\"],\"articleSection\":[\"Android\",\"iOS\",\"Mobile\",\"Mobile Architecture and Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/\",\"name\":\"SOLID: Interface Segregation Principle - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/InterfaceSegregationPrinciple.jpg\",\"datePublished\":\"2017-11-22T08:01:07+00:00\",\"dateModified\":\"2017-11-22T08:13:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/InterfaceSegregationPrinciple.jpg\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/InterfaceSegregationPrinciple.jpg\",\"width\":750,\"height\":600},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/solid-interface-segregation-principle\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SOLID: Interface Segregation Principle\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SOLID: Interface Segregation Principle - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/","og_locale":"en_US","og_type":"article","og_title":"SOLID: Interface Segregation Principle - InnovationM - Blog","og_description":"Interface Segregation Principle Previous Principle:\u00a0Liskov&#8217;s Substitution Principle A client should never be forced to implement an interface that it doesn\u2019t use or clients shouldn\u2019t be forced to depend on methods they do not use. In OOD we provide abstraction to module using interfaces, so we create an interface for a module and an implementation class. [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/","og_site_name":"InnovationM - Blog","article_published_time":"2017-11-22T08:01:07+00:00","article_modified_time":"2017-11-22T08:13:15+00:00","og_image":[{"width":750,"height":600,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/11\/InterfaceSegregationPrinciple.jpg","type":"image\/jpeg"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"SOLID: Interface Segregation Principle","datePublished":"2017-11-22T08:01:07+00:00","dateModified":"2017-11-22T08:13:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/"},"wordCount":234,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/11\/InterfaceSegregationPrinciple.jpg","keywords":["Design Pattern","Mobile","Mobile Design"],"articleSection":["Android","iOS","Mobile","Mobile Architecture and Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/","url":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/","name":"SOLID: Interface Segregation Principle - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/11\/InterfaceSegregationPrinciple.jpg","datePublished":"2017-11-22T08:01:07+00:00","dateModified":"2017-11-22T08:13:15+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/11\/InterfaceSegregationPrinciple.jpg","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/11\/InterfaceSegregationPrinciple.jpg","width":750,"height":600},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/solid-interface-segregation-principle\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SOLID: Interface Segregation Principle"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/3876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=3876"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/3876\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/3889"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=3876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=3876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=3876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}