{"id":61,"date":"2011-09-19T14:16:43","date_gmt":"2011-09-19T14:16:43","guid":{"rendered":"http:\/\/www.lexicalscope.com\/blog\/?p=61"},"modified":"2011-09-19T14:40:07","modified_gmt":"2011-09-19T14:40:07","slug":"dependency-inversion","status":"publish","type":"post","link":"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/19\/dependency-inversion\/","title":{"rendered":"Dependency Inversion"},"content":{"rendered":"<h3>What is a Dependency<\/h3>\n<p>For the purposes of this discussion a class <code>A<\/code> has a dependency on another class <code>B<\/code>, iff you cannot compile <code>class A<\/code> without <code>class B<\/code>.<\/p>\n<p>Example<\/p>\n<pre lang=\"java\">\r\nclass A \u000b{\u000bB b;\u000b}\u000b\u000b\r\nclass B\u000b { \u000b}\r\n<\/pre>\n<p><a href=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonB.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"62\" data-permalink=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/19\/dependency-inversion\/aonb\/\" data-orig-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonB.png\" data-orig-size=\"246,46\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"A depends on B\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonB.png\" src=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonB.png\" alt=\"Box A with an arrow pointing to box B\" title=\"A depends on B\" width=\"246\" height=\"46\" class=\"aligncenter size-full wp-image-62\" \/><\/a><\/p>\n<p><strong>We have to compile <code>class B<\/code> before (or at the same time as) <code>class A<\/code>.<\/strong><\/p>\n<h3>Other kinds of Dependency<\/h3>\n<p>Anywhere the name &#8220;<code>B<\/code>&#8221; appears in <code>class A<\/code> creates a dependency. Some other examples of dependencies are:<\/p>\n<pre lang=\"java\">\r\nclass A extends B \u000b{ \u000b}\r\nclass A implements B\u000b { \u000b}\r\n\u000b\u000bclass A \u000b{\u000b void method(B b) { } \u000b}\r\n<\/pre>\n<h3>Transitive Dependencies<\/h3>\n<p>If a <code>class A<\/code> depends on another <code>class B<\/code> which itself has dependencies then the dependencies of <code>class B<\/code> are effectively dependencies of <code>class A<\/code><\/p>\n<p>Example:<\/p>\n<pre lang=\"java\">\r\nclass A\u000b { B b;\u000b }\r\n\u000b\u000bclass B \u000b{ C c;\u000b } \u000b\r\nclass C \u000b{\u000b \u000b}\r\n<\/pre>\n<p><a href=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonBonC.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"66\" data-permalink=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/19\/dependency-inversion\/aonbonc\/\" data-orig-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonBonC.png\" data-orig-size=\"421,69\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"A transitive dependency on C\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonBonC.png\" src=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonBonC-300x49.png\" alt=\"Box A with an arrow to Box B with an Arrow to Box C, dotted arrow from Box A to Box C\" title=\"A transitive dependency on C\" width=\"300\" height=\"49\" class=\"aligncenter size-medium wp-image-66\" srcset=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonBonC-300x49.png 300w, https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/AonBonC.png 421w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>We have to compile <code>class C<\/code> before (or at the same time as) <code>class B<\/code> and <code>class A<\/code>.<\/strong><\/p>\n<h3>The Problem<\/h3>\n<p>When <code>class C<\/code> changes, we have to recompile and retest both <code>class A<\/code> and <code>class B<\/code>. In a large system this can take a very long time. It also means that you have to know about <code>class C<\/code> in advance; you cannot decide on <code>class C<\/code> after deciding on <code>class A<\/code>.<\/p>\n<p>If <code>class C<\/code> is a more concrete class then it might change more frequently than <code>class A<\/code>. This will cause <code>class A<\/code> to be recompiled\/tested much more frequently than it otherwise would need to be.<\/p>\n<h3>The Solution<\/h3>\n<h4>Inverting Dependencies<\/h4>\n<p>Have <code>class A<\/code> and <code>class B<\/code> both depend on an abstraction <code>I<\/code>. This inverts the direction of the dependency arrow on <code>class B<\/code>.<\/p>\n<pre lang=\"java\">\r\ninterface I { \u000b}\r\nclass A \u000b{ I i; \u000b}\r\nclass B implements I \u000b{ \u000b}\r\n<\/pre>\n<p><a href=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnB.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"71\" data-permalink=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/19\/dependency-inversion\/anotonb\/\" data-orig-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnB.png\" data-orig-size=\"221,189\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"A dependency on B inverted\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnB.png\" src=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnB.png\" alt=\"Box A with arrow to Box I, Box B with arrow to Box I\" title=\"A dependency on B inverted\" width=\"221\" height=\"189\" class=\"aligncenter size-full wp-image-71\" \/><\/a><\/p>\n<h4>Breaks Transitive Dependency<\/h4>\n<p>The really helpful effect of this inversion is that it also breaks the transitive dependency from <code>class A<\/code> onto <code>class B<\/code><\/p>\n<pre lang=\"java\">\r\ninterface I\u000b{\u000b }\r\nclass A \u000b{\u000b I i;\u000b }\r\nc\u000blass B implements I\u000b{ C c; \u000b}\r\nc\u000blass C\u000b { \u000b}\r\n<\/pre>\n<p><a href=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnBOnC.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"76\" data-permalink=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/19\/dependency-inversion\/anotonbonc\/\" data-orig-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnBOnC.png\" data-orig-size=\"357,276\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"A dependency on B inverted means no dependency C\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnBOnC.png\" src=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnBOnC-300x231.png\" alt=\"Box A arrow to Box I, Box B arrow to Box I, Box B arrow to box C. Box A dotted arrow to Box C deleted\" title=\"A dependency on B inverted means no dependency on C\" width=\"300\" height=\"231\" class=\"aligncenter size-medium wp-image-76\" srcset=\"https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnBOnC-300x231.png 300w, https:\/\/www.lexicalscope.com\/blog\/wp-content\/uploads\/2011\/09\/ANotOnBOnC.png 357w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>Dependency Inversion Principle<\/h3>\n<p>This is an application of the <a href=\"http:\/\/www.objectmentor.com\/resources\/articles\/dip.pdf\" title=\"Dependency Inversion Principle\">Dependency Inversion Principle<\/a>:<\/p>\n<ul>\n<li>High level modules should not depend on low level modules. Both should depend on abstractions.<\/li>\n<li>Abstractions should not depend upon details. Details should depend upon abstractions.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is a Dependency For the purposes of this discussion a class A has a dependency on another class B, iff you cannot compile class A without class B. Example class A \u000b{\u000bB b;\u000b}\u000b\u000b class B\u000b { \u000b} We have &hellip; <a href=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/19\/dependency-inversion\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,8],"tags":[],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-patterns","category-refactoring"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2e3P7-Z","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":10,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/61\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}