{"id":2122,"date":"2024-06-20T03:32:37","date_gmt":"2024-06-20T03:32:37","guid":{"rendered":"https:\/\/aryalspace.com\/?p=2122"},"modified":"2024-06-20T03:32:38","modified_gmt":"2024-06-20T03:32:38","slug":"inversion-of-control-ioc","status":"publish","type":"post","link":"https:\/\/aryalspace.com\/?p=2122","title":{"rendered":"Inversion of Control (IoC)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Inversion of Control (IoC) is a principle in software engineering where the control flow of a program is inverted compared to traditional procedural programming. Instead of the program dictating the flow of control, it delegates this responsibility to a framework or container. This allows for more flexible and decoupled code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To understand IoC, let\u2019s imagine a simple scenario involving a guitar player and a concert:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Traditional Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In a traditional approach, the guitar player would be responsible for organizing everything:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Finding a venue.<\/li>\n\n\n\n<li>Promoting the concert.<\/li>\n\n\n\n<li>Selling tickets.<\/li>\n\n\n\n<li>Performing.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the guitar player has full control over all aspects of the concert.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">IoC Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With IoC, the guitar player delegates many responsibilities to a concert organizer:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The organizer finds the venue.<\/li>\n\n\n\n<li>The organizer promotes the concert.<\/li>\n\n\n\n<li>The organizer sells tickets.<\/li>\n\n\n\n<li>The guitar player only needs to focus on performing.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the control is inverted: the concert organiser (the framework or container) manages the overall flow, and the guitar player (the specific component) performs a specific role when called upon.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Example in Software Development<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine a software application that sends notifications. In a traditional approach, the notification service might directly instantiate and manage the email and SMS services:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scenario<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine we have a\u00a0<code>NotificationService<\/code>\u00a0that needs to send notifications via email and SMS. Instead of the\u00a0<code>NotificationService<\/code>\u00a0creating these dependencies itself, we\u2019ll inject them from the outside.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Traditional Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In a traditional approach, the\u00a0<code>NotificationService<\/code>\u00a0would create and manage its dependencies directly:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>public class EmailService {\n    public void sendEmail(String message) {\n        \/\/ logic to send email\n        System.out.println(\"Email sent: \" + message);\n    }\n}\n\npublic class SMSService {\n    public void sendSMS(String message) {\n        \/\/ logic to send SMS\n        System.out.println(\"SMS sent: \" + message);\n    }\n}\n\npublic class NotificationService {\n    private EmailService emailService;\n    private SMSService smsService;\n\n    public NotificationService() {\n        this.emailService = new EmailService();\n        this.smsService = new SMSService();\n    }\n\n    public void sendNotification(String message) {\n        emailService.sendEmail(message);\n        smsService.sendSMS(message);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">IoC with Dependency Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Using Dependency Injection, we\u2019ll inject&nbsp;<code>EmailService<\/code>&nbsp;and&nbsp;<code>SMSService<\/code>&nbsp;into&nbsp;<code>NotificationService<\/code>. We\u2019ll also use a simple dependency injection framework like Spring for illustration.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Define Interfaces<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">First, we define interfaces for the services to decouple the implementations from the\u00a0<code>NotificationService<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>public interface EmailService {\n    void sendEmail(String message);\n}\n\npublic interface SMSService {\n    void sendSMS(String message);\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Implement the Interfaces<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we implement these interfaces:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>public class EmailServiceImpl implements EmailService {\n    @Override\n    public void sendEmail(String message) {\n        \/\/ logic to send email\n        System.out.println(\"Email sent: \" + message);\n    }\n}\n\npublic class SMSServiceImpl implements SMSService {\n    @Override\n    public void sendSMS(String message) {\n        \/\/ logic to send SMS\n        System.out.println(\"SMS sent: \" + message);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Create NotificationService with DI<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we create the\u00a0<code>NotificationService<\/code>\u00a0that uses dependency injection:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>public class NotificationService {\n    private EmailService emailService;\n    private SMSService smsService;\n\n    public NotificationService(EmailService emailService, SMSService smsService) {\n        this.emailService = emailService;\n        this.smsService = smsService;\n    }\n\n    public void sendNotification(String message) {\n        emailService.sendEmail(message);\n        smsService.sendSMS(message);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Configure Dependencies<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Using Spring Framework to wire everything together, we define the beans in a configuration class:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class AppConfig {\n\n    @Bean\n    public EmailService emailService() {\n        return new EmailServiceImpl();\n    }\n\n    @Bean\n    public SMSService smsService() {\n        return new SMSServiceImpl();\n    }\n\n    @Bean\n    public NotificationService notificationService() {\n        return new NotificationService(emailService(), smsService());\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><br><br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5: Use the ApplicationContext to Get Beans<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we use the Spring application context to get the\u00a0<code>NotificationService<\/code>\u00a0bean and send a notification:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import org.springframework.context.ApplicationContext;\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);\n\n        NotificationService notificationService = context.getBean(NotificationService.class);\n        notificationService.sendNotification(\"Hello, this is a test notification!\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By using IoC and Dependency Injection, the&nbsp;<code>NotificationService<\/code>&nbsp;no longer creates its own dependencies. Instead, these dependencies are provided by an external configuration, making the code more flexible, easier to test, and maintainable. This approach leverages the power of frameworks like Spring to manage dependencies and control the flow of the application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inversion of Control (IoC) is a principle in software engineering where the control flow of a program is inverted compared to traditional procedural programming. Instead of the program dictating the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-2122","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Inversion of Control (IoC) - Aryal Space - Magic of Technology and Beings<\/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:\/\/aryalspace.com\/?p=2122\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inversion of Control (IoC) - Aryal Space - Magic of Technology and Beings\" \/>\n<meta property=\"og:description\" content=\"Inversion of Control (IoC) is a principle in software engineering where the control flow of a program is inverted compared to traditional procedural programming. Instead of the program dictating the&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/aryalspace.com\/?p=2122\" \/>\n<meta property=\"og:site_name\" content=\"Aryal Space - Magic of Technology and Beings\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/bikramaryal\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-20T03:32:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-20T03:32:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/aryalspace.com\/wp-content\/uploads\/2023\/08\/2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1281\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"aryalspace\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bikramaryal\" \/>\n<meta name=\"twitter:site\" content=\"@bikramaryal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"aryalspace\" \/>\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:\/\/aryalspace.com\/?p=2122#article\",\"isPartOf\":{\"@id\":\"https:\/\/aryalspace.com\/?p=2122\"},\"author\":{\"name\":\"aryalspace\",\"@id\":\"https:\/\/aryalspace.com\/#\/schema\/person\/2ad5cea3af89987c847dcafdf835100b\"},\"headline\":\"Inversion of Control (IoC)\",\"datePublished\":\"2024-06-20T03:32:37+00:00\",\"dateModified\":\"2024-06-20T03:32:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/aryalspace.com\/?p=2122\"},\"wordCount\":409,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/aryalspace.com\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/aryalspace.com\/?p=2122#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/aryalspace.com\/?p=2122\",\"url\":\"https:\/\/aryalspace.com\/?p=2122\",\"name\":\"Inversion of Control (IoC) - Aryal Space - Magic of Technology and Beings\",\"isPartOf\":{\"@id\":\"https:\/\/aryalspace.com\/#website\"},\"datePublished\":\"2024-06-20T03:32:37+00:00\",\"dateModified\":\"2024-06-20T03:32:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/aryalspace.com\/?p=2122#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/aryalspace.com\/?p=2122\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/aryalspace.com\/?p=2122#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/aryalspace.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inversion of Control (IoC)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/aryalspace.com\/#website\",\"url\":\"https:\/\/aryalspace.com\/\",\"name\":\"Aryal Space - Blog and Programming Tutorials\",\"description\":\"Exploring the Cosmos of Code: Aryal Space - Where Blogs and Programming Unite!\",\"publisher\":{\"@id\":\"https:\/\/aryalspace.com\/#organization\"},\"alternateName\":\"Elevate Your Code Horizons with Aryal Space: Ignite, Inspire, Innovate!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/aryalspace.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/aryalspace.com\/#organization\",\"name\":\"Aryal Space\",\"alternateName\":\"AryalSpace\",\"url\":\"https:\/\/aryalspace.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/aryalspace.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/aryalspace.com\/wp-content\/uploads\/2023\/08\/Aryal-Space.png\",\"contentUrl\":\"https:\/\/aryalspace.com\/wp-content\/uploads\/2023\/08\/Aryal-Space.png\",\"width\":1000,\"height\":1000,\"caption\":\"Aryal Space\"},\"image\":{\"@id\":\"https:\/\/aryalspace.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/bikramaryal\",\"https:\/\/x.com\/bikramaryal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/aryalspace.com\/#\/schema\/person\/2ad5cea3af89987c847dcafdf835100b\",\"name\":\"aryalspace\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/aryalspace.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/11fd1e0ebb393929b1f13bd515f4a6a61545abcd6c2ddc9323fe52fda1d3354c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/11fd1e0ebb393929b1f13bd515f4a6a61545abcd6c2ddc9323fe52fda1d3354c?s=96&d=mm&r=g\",\"caption\":\"aryalspace\"},\"sameAs\":[\"https:\/\/aryalspace.com\"],\"url\":\"https:\/\/aryalspace.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inversion of Control (IoC) - Aryal Space - Magic of Technology and Beings","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:\/\/aryalspace.com\/?p=2122","og_locale":"en_US","og_type":"article","og_title":"Inversion of Control (IoC) - Aryal Space - Magic of Technology and Beings","og_description":"Inversion of Control (IoC) is a principle in software engineering where the control flow of a program is inverted compared to traditional procedural programming. Instead of the program dictating the&hellip;","og_url":"https:\/\/aryalspace.com\/?p=2122","og_site_name":"Aryal Space - Magic of Technology and Beings","article_publisher":"https:\/\/facebook.com\/bikramaryal","article_published_time":"2024-06-20T03:32:37+00:00","article_modified_time":"2024-06-20T03:32:38+00:00","og_image":[{"width":1920,"height":1281,"url":"https:\/\/aryalspace.com\/wp-content\/uploads\/2023\/08\/2.jpg","type":"image\/jpeg"}],"author":"aryalspace","twitter_card":"summary_large_image","twitter_creator":"@bikramaryal","twitter_site":"@bikramaryal","twitter_misc":{"Written by":"aryalspace","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/aryalspace.com\/?p=2122#article","isPartOf":{"@id":"https:\/\/aryalspace.com\/?p=2122"},"author":{"name":"aryalspace","@id":"https:\/\/aryalspace.com\/#\/schema\/person\/2ad5cea3af89987c847dcafdf835100b"},"headline":"Inversion of Control (IoC)","datePublished":"2024-06-20T03:32:37+00:00","dateModified":"2024-06-20T03:32:38+00:00","mainEntityOfPage":{"@id":"https:\/\/aryalspace.com\/?p=2122"},"wordCount":409,"commentCount":0,"publisher":{"@id":"https:\/\/aryalspace.com\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/aryalspace.com\/?p=2122#respond"]}]},{"@type":"WebPage","@id":"https:\/\/aryalspace.com\/?p=2122","url":"https:\/\/aryalspace.com\/?p=2122","name":"Inversion of Control (IoC) - Aryal Space - Magic of Technology and Beings","isPartOf":{"@id":"https:\/\/aryalspace.com\/#website"},"datePublished":"2024-06-20T03:32:37+00:00","dateModified":"2024-06-20T03:32:38+00:00","breadcrumb":{"@id":"https:\/\/aryalspace.com\/?p=2122#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/aryalspace.com\/?p=2122"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/aryalspace.com\/?p=2122#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/aryalspace.com\/"},{"@type":"ListItem","position":2,"name":"Inversion of Control (IoC)"}]},{"@type":"WebSite","@id":"https:\/\/aryalspace.com\/#website","url":"https:\/\/aryalspace.com\/","name":"Aryal Space - Blog and Programming Tutorials","description":"Exploring the Cosmos of Code: Aryal Space - Where Blogs and Programming Unite!","publisher":{"@id":"https:\/\/aryalspace.com\/#organization"},"alternateName":"Elevate Your Code Horizons with Aryal Space: Ignite, Inspire, Innovate!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/aryalspace.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/aryalspace.com\/#organization","name":"Aryal Space","alternateName":"AryalSpace","url":"https:\/\/aryalspace.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aryalspace.com\/#\/schema\/logo\/image\/","url":"https:\/\/aryalspace.com\/wp-content\/uploads\/2023\/08\/Aryal-Space.png","contentUrl":"https:\/\/aryalspace.com\/wp-content\/uploads\/2023\/08\/Aryal-Space.png","width":1000,"height":1000,"caption":"Aryal Space"},"image":{"@id":"https:\/\/aryalspace.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/bikramaryal","https:\/\/x.com\/bikramaryal"]},{"@type":"Person","@id":"https:\/\/aryalspace.com\/#\/schema\/person\/2ad5cea3af89987c847dcafdf835100b","name":"aryalspace","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aryalspace.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/11fd1e0ebb393929b1f13bd515f4a6a61545abcd6c2ddc9323fe52fda1d3354c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/11fd1e0ebb393929b1f13bd515f4a6a61545abcd6c2ddc9323fe52fda1d3354c?s=96&d=mm&r=g","caption":"aryalspace"},"sameAs":["https:\/\/aryalspace.com"],"url":"https:\/\/aryalspace.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/aryalspace.com\/index.php?rest_route=\/wp\/v2\/posts\/2122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aryalspace.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aryalspace.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aryalspace.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aryalspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2122"}],"version-history":[{"count":1,"href":"https:\/\/aryalspace.com\/index.php?rest_route=\/wp\/v2\/posts\/2122\/revisions"}],"predecessor-version":[{"id":2123,"href":"https:\/\/aryalspace.com\/index.php?rest_route=\/wp\/v2\/posts\/2122\/revisions\/2123"}],"wp:attachment":[{"href":"https:\/\/aryalspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aryalspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aryalspace.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}