{"id":1873,"date":"2016-10-11T09:18:35","date_gmt":"2016-10-11T09:18:35","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1873"},"modified":"2019-02-25T05:20:42","modified_gmt":"2019-02-25T05:20:42","slug":"invesrion-of-controlioc","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/invesrion-of-controlioc\/","title":{"rendered":"Invesrion of Control(IoC)"},"content":{"rendered":"<p><strong>IoC<\/strong> is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application.<\/p>\n<p>Say the Excel Jar files with utility methods used the application uses the methods in the Utility Class and the flow is controlled by the way the method gets called in order the get the things done.<\/p>\n<p><em>whereas<\/em><\/p>\n<p>In framework like spring the implementation is defined in XML files and by using annotation and the framework calls the methods as per defined in xml.<\/p>\n<p><strong>Without Ioc<\/strong><\/p>\n<pre>\r\n  Application -> Methods -> Framework      \r\n<\/pre>\n<p><strong>With Ioc<\/strong><\/p>\n<pre>\r\n  Framework -> XML File ->  Method Call (or) Implementation      \r\n<\/pre>\n<p><strong>Inversion of Control(IoC) Container:<\/strong><br \/>\nCommon characteristic of frameworks IOC manages java objects<\/p>\n<ol>\n<li>From instantiation to destruction through its BeanFactory. <\/li>\n<li>Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean&#8217;s scope, lifecycle events, and any AOP features for which it has been configured and coded.\n<\/li>\n<\/ol>\n<p><strong>Flow of control is &#8220;inverted&#8221; by dependency injection because you have effectively delegated dependencies to some external system <\/strong><\/p>\n<p>The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.<\/p>\n<p>For example, say your application has a text editor component and you want to provide spell checking. Your standard code would look something like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class TextEditor\r\n{\r\n    private SpellChecker checker;\r\n    public TextEditor()\r\n    {\r\n        this.checker = new SpellChecker();\r\n    }\r\n}\r\n<\/pre>\n<p>What we&#8217;ve done here is create a dependency between the TextEditor and the SpellChecker. In an IoC scenario we would instead do something like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class TextEditor\r\n{\r\n    private ISpellChecker checker;\r\n    public TextEditor(ISpellChecker checker)\r\n    {\r\n        this.checker = checker;\r\n    }\r\n}\r\n<\/pre>\n<p>Now, the client creating the TextEditor class has the control over which SpellChecker implementation to use. We&#8217;re injecting the TextEditor with the dependency.<\/p>\n<p><strong>Without IoC:<\/strong> you ask for &#8220;apple&#8221;, and you are always served apple when you ask more.<br \/>\n<strong><br \/>\nWith IoC:<\/strong> You can ask for &#8220;fruit&#8221;. You can get different fruits each time you get served. for example, apple, orange, or water melon. <\/p>\n<p>Inversion of Control, (or IoC), is about getting<br \/>\n<strong>freedom <\/strong>(You get married, you lost freedom and you are being controlled. You divorced, you have just implemented Inversion of Control. That&#8217;s what we called, <strong>&#8220;decoupled&#8221;<\/strong>. Good computer system discourages some very close relationship.) <\/p>\n<p><strong>flexibility<\/strong> (The kitchen in your office only serves clean tap water, that is your only choice when you want to drink. Your boss implemented Inversion of Control by setting up a new coffee machine. Now you get the flexibility of choosing either tap water or coffee.) <\/p>\n<p><strong>less dependency<\/strong> (Your partner has a job, you don&#8217;t have a job, you financially depend on your partner, so you are controlled. You find a job, you have implemented Inversion of Control. Good computer system encourages in-dependency.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application. Say the Excel Jar files with utility methods used the application uses the methods in the Utility Class and the flow is controlled by the way the method gets called&hellip; <a href=\"https:\/\/codethataint.com\/blog\/invesrion-of-controlioc\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211],"tags":[253],"class_list":["post-1873","post","type-post","status-publish","format-standard","hentry","category-theory","tag-interview"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1873"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1873\/revisions"}],"predecessor-version":[{"id":1876,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1873\/revisions\/1876"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}