{"id":1701,"date":"2021-03-06T14:04:50","date_gmt":"2021-03-06T13:04:50","guid":{"rendered":"http:\/\/mateuszglowinski.pl\/?p=1701"},"modified":"2021-03-06T16:01:39","modified_gmt":"2021-03-06T15:01:39","slug":"dependency-inversion-principle","status":"publish","type":"post","link":"https:\/\/mateuszglowinski.pl\/index.php\/2021\/03\/06\/dependency-inversion-principle\/","title":{"rendered":"Dependency Inversion Principle"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1701\" class=\"elementor elementor-1701\" 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-33068e0 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"33068e0\" 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-d18f7ac\" data-id=\"d18f7ac\" 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-a4ac8f9 elementor-widget elementor-widget-text-editor\" data-id=\"a4ac8f9\" 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;Dependency Inversion Principle&#8221; (DIP) ?<\/h4><p>It is the <span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">fifth<\/span><\/span> rule of the SOLID design rules (it stands for D 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>High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details.<\/p><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>, <\/span><\/span><span lang=\"en\"><span data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">dependencies should depend on abstraction as much as possible and not on a specific type.<\/span><\/span><\/p><h4>Real-life example<\/h4><p>Let\u2019s say we have an vet application that check health of animals.<\/p><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Launcher {\n    public static void main(String[] args) {\n        Vet vet = new Vet();\n        vet.checkHealth(AnimalType.dog);\n        vet.checkHealth(AnimalType.cat);\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public enum AnimalType {\n\n    dog,\n    cat\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Dog {\n\n    public void checkHealth() {\n\n        System.out.println(\"Checking dog health\");\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Cat {\n\n    public void checkHealth() {\n\n        System.out.println(\"Checking cat health\");\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Vet {\n\n    private final Dog dog;\n    private final Cat cat;\n\n    public Vet() {\n\n        this.dog = new Dog();\n        this.cat = new Cat();\n    }\n\n    public void checkHealth(AnimalType animalType) {\n\n        switch (animalType) {\n\n            case dog:\n                this.dog.checkHealth();\n                break;\n            case cat:\n                this.cat.checkHealth();\n                break;\n            default:\n                break;\n        }\n    }\n}\n<\/pre><p>UML diagram of our application:<\/p><p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1768\" src=\"http:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-no-DIP.png\" alt=\"\" width=\"491\" height=\"334\" srcset=\"https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-no-DIP.png 491w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-no-DIP-300x204.png 300w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-no-DIP-160x109.png 160w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/p><p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"1\">The <em>Vet<\/em> class is closely related to the <em>Dog<\/em> and <em>Cat<\/em> classes, it cannot exist without these two classes, due to that out program does not meet the <em>DIP<\/em> rule. <\/span><\/span><\/p><p>We can fix it by removing\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Object_composition\" target=\"_blank\" rel=\"noopener\">composition<\/a> in Vet class.<\/p><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Launcher {\n    public static void main(String[] args) {\n\n        Vet vet = new Vet();\n        vet.checkHealth(new Dog());\n        vet.checkHealth(new Cat());\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public interface Animal {\n    void checkHealth();\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Dog implements Animal {\n\n    public void checkHealth() {\n\n        System.out.println(\"Checking dog health\");\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Cat implements Animal {\n\n    public void checkHealth() {\n\n        System.out.println(\"Checking cat health\");\n    }\n}\n<\/pre><pre style=\"background-color: lightgrey; color: black; border-radius: 5px;\">public class Vet {\n\n    public void checkHealth(Animal animal) {\n        animal.checkHealth();\n    }\n}\n<\/pre><p>Now, <em>Vet<\/em> class can exists without any other class. Moreover <em>checkHealth<\/em> method depends from abstraction, <span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"pl\" data-phrase-index=\"0\">which is in line with the <em>DIP<\/em> rule.<br \/><\/span><\/span><\/p><p>UML diagram of our application after refactoring:<\/p><p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1769\" src=\"http:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-with-DIP.png\" alt=\"\" width=\"491\" height=\"478\" srcset=\"https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-with-DIP.png 491w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-with-DIP-300x292.png 300w, https:\/\/mateuszglowinski.pl\/wp-content\/uploads\/2021\/03\/Vet-app-with-DIP-160x156.png 160w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/p><h4>Why should I follow &#8222;Dependency Inversion Principle&#8221; ?<\/h4><p>This rule helps to reduce the number of dependencies among modules and thanks to that code is easier to extend and maintain. Moreover classes are not tightly coupled with the lower-level objects and we can easily reuse the logic from the high-level modules.<\/p><h4>What to look for?<\/h4><p><em>DIP <\/em>is the rule, which is really common while creating libraries\/packages. It is strongly recommended, that such a code does not require an outside composition.<\/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;Dependency Inversion Principle&#8221; (DIP) ? It is the fifth rule of the SOLID design rules (it stands for D 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\/1701"}],"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=1701"}],"version-history":[{"count":61,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1701\/revisions"}],"predecessor-version":[{"id":1793,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/posts\/1701\/revisions\/1793"}],"wp:attachment":[{"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mateuszglowinski.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}