{"id":471,"date":"2020-09-06T16:53:19","date_gmt":"2020-09-06T13:53:19","guid":{"rendered":"https:\/\/blog.gunlerveisler.gen.tr\/?p=471"},"modified":"2020-09-27T22:01:34","modified_gmt":"2020-09-27T19:01:34","slug":"dependency-injection","status":"publish","type":"post","link":"https:\/\/aliyargunes.com.tr\/blog\/dependency-injection\/","title":{"rendered":"A Naive Example of Dependency Injection"},"content":{"rendered":"\n<p>&#8211;&gt; It&#8217;s a way in which you decouple the conventional dependency relationships between objects.<\/p>\n\n\n\n<p>Say we have two objects that are related to each other, one is dependent on the other. The idea is decouple this dependency. So that they&#8217;re not tied to each other.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><a href=\"https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.gunlerveisler.gen.tr\/wp-content\/uploads\/2020\/09\/class-diagram.png\" alt=\"\" class=\"wp-image-475\" width=\"226\" height=\"71\" srcset=\"https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram.png 905w, https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram-300x94.png 300w, https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram-768x241.png 768w\" sizes=\"(max-width: 226px) 100vw, 226px\" \/><\/a><figcaption>Two Different Classes<\/figcaption><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nCircle c = new Circle();\nc.draw();\n\nTriangle t = new Triangle();\nt.draw();\n<\/pre><\/div>\n\n\n<p>We can use polymorphism:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><a href=\"https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.gunlerveisler.gen.tr\/wp-content\/uploads\/2020\/09\/class-diagram-2.png\" alt=\"\" class=\"wp-image-478\" width=\"242\" height=\"208\" srcset=\"https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram-2.png 969w, https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram-2-300x258.png 300w, https:\/\/aliyargunes.com.tr\/blog\/wp-content\/uploads\/2020\/09\/class-diagram-2-768x659.png 768w\" sizes=\"(max-width: 242px) 100vw, 242px\" \/><\/a><figcaption>Realization: Circle and Triangle implements Shape interface.<\/figcaption><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nShape s1 = new Circle();\ns1.draw();\n\nShape s2 = new Triangle();\ns2.draw();\n<\/pre><\/div>\n\n\n<p>Still tying the code to a triangle or a circle. It still hard coded to use either a triangle or a circle.<\/p>\n\n\n\n<!--more-->\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/\/ Applicaiton Class\n\npublic void myDrawMethod(Shape shape) {\nshape.draw();\n}\n\n\/\/ Somewhere else in the class\n\nShape shape = new Triangle();\nmyDrawMethod(shape);\n<\/pre><\/div>\n\n\n<p>We still tied to this new Triangle, still not free from it. Somewhere else in the class we need to instantiation.<\/p>\n\n\n\n<p><strong>Class Member Variable:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class Drawing {\n\n private Shape shape;\n\n   public void setShape(Shape shape) {\n    this.shape = shape;\n   }\n\n   public void drawShape() {\n    this.shape.draw();\n   }\n\n}\n<\/pre><\/div>\n\n\n<p>Now totally removed the dependency from Triangle or Circle. If you want to draw a triangle or circle we don&#8217;t have to modify the drawing class. This is going to remain the same, all we need to do is pass a triangle or circle to the setter.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/\/ Different Class\n\nTriangle t = new Triangle();\nDrawing d = new Drawing();\nd.setShape(t);\nd.drawShape();\n<\/pre><\/div>\n\n\n<p>Advantage is here is that in Drawing class instead of having an instance of a specific shape as a triangle or a circle, it has an instance of the parent shape object.<\/p>\n\n\n\n<p>We are separating the whole dependency out of a class, the Drawing class doesn&#8217;t really know what its dependent on, it really doesn&#8217;t know what it&#8217;s drawing and the advantage of this is that if what it has to draw changes we don&#8217;t have to modify the Drawing class.<\/p>\n\n\n\n<p>Dependency to the triangle is actually injected to the Drawing class by something else, it&#8217;s injected by a completely different class. This is the principle of dependency injection.<\/p>\n\n\n\n<p>We just tell Spring saying here have this object (Triangle or Circle), now inject this dependency to this object. We just call the <code>drawShape()<\/code> and then configure Spring to inject the right dependencies to the right objects and Spring takes care of that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8211;&gt; It&#8217;s a way in which you decouple the conventional dependency relationships between objects. Say we have two objects that are related to each other, one is dependent on the other. The idea is decouple this dependency. So that they&#8217;re &hellip; <a href=\"https:\/\/aliyargunes.com.tr\/blog\/dependency-injection\/\">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":{"footnotes":""},"categories":[94,8],"tags":[95,96],"class_list":["post-471","post","type-post","status-publish","format-standard","hentry","category-spring-framework","category-works","tag-dependency-injection","tag-spring-framework"],"_links":{"self":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/471"}],"collection":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=471"}],"version-history":[{"count":18,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":705,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/471\/revisions\/705"}],"wp:attachment":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}