{"id":6233,"date":"2015-10-23T09:00:30","date_gmt":"2015-10-23T13:00:30","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6233"},"modified":"2017-08-11T13:22:54","modified_gmt":"2017-08-11T17:22:54","slug":"doctor-do-i-need-a-dependency-injection","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/10\/23\/doctor-do-i-need-a-dependency-injection\/","title":{"rendered":"Doctor, Do I Need a Dependency Injection?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\">Greg<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52394-matlab-dependency-injection\">MATLAB Dependency Injection<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6581434-matt-mcdonnell\">Matt McDonnell<\/a>.\r\n      <\/p>\r\n      <p>Imagine you want to build an application that dresses you in the morning.<\/p>\r\n      <p>What you wear on any given day will depend on the weather that day. If it's raining, you might wear a rain slicker, rain boots,\r\n         and carry an umbrella.  If it's bright and sunny you might not wear a jacket at all, wear sneakers, and ditch the umbrella.\r\n      <\/p>\r\n      <p>How do you build an application that can perform the right operations with a large number of different scenarios.<\/p>\r\n      <p>One methodology you can apply is dependency injection.<\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Dependency Whaaaaa?<\/a><\/li>\r\n         <li><a href=\"#2\">You've Seen This Somewhere Before<\/a><\/li>\r\n         <li><a href=\"#3\">Why Did I Choose This File Exchange Entry<\/a><\/li>\r\n         <li><a href=\"#4\">Do You Employ Dependency Injection?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Dependency Whaaaaa?<a name=\"1\"><\/a><\/h3>\r\n   <p><tt>\"Dependency injection is a technique for constructing an application from components without the individual components needing\r\n         to be coupled to a particular implementation of other components.\"<\/tt><\/p>\r\n   <p>I stole this right out of the top of the documentation for the \"demo_mdepin\" function in the File Exchange entry.<\/p>\r\n   <p><i><b>But wait... what does that even mean?<\/b><\/i><\/p>\r\n   <p>Basically this refers to the notion of software plugins.  You have a generic application code that is customized by applying\r\n      different specific implementations.\r\n   <\/p>\r\n   <p>This enables you to configure the application based on selection criteria like, is it raining.<\/p>\r\n   <p>The modular and independent nature of the components also enables more simplified testing strategies.<\/p>\r\n   <h3>You've Seen This Somewhere Before<a name=\"2\"><\/a><\/h3>\r\n   <p>You might be thinking \"hmmm, this seems like using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/function-handles.html\">function handles<\/a> in MATLAB.\"\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">if<\/span> isRaining\r\n  coatFunction = @putOnRainSlicker;\r\n<span style=\"color: #0000FF\">else<\/span>\r\n  coatFunction = @putOnSequinedJeanJacket;\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">getDressed(coatFunction);<\/pre><p>Dependency injection itself is more than just passing around handles to different functions. However the above illustrates\r\n      the key concept that you can vary implementation by determining which function gets called through some configuration.\r\n   <\/p>\r\n   <p>But you're not just changing which coat you are wearing when it is raining. You might have completely different clothing needs\r\n      depending on the weather.\r\n   <\/p>\r\n   <p>It's more likely you would do something like:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">config = struct();\r\nconfig.dressForRain.getDressed = {<span style=\"color: #A020F0\">'putOnRainSlicker'<\/span>\r\n                                  <span style=\"color: #A020F0\">'putOnRainBoots'<\/span>\r\n                                  <span style=\"color: #A020F0\">'grabUmbrella'<\/span>};\r\nconfig.dressForRain.putOnRainSlicker = putOnJacket(<span style=\"color: #A020F0\">'rainSlicker'<\/span>);\r\nconfig.dressForRain.putOnRainBoots = <span style=\"color: #0000FF\">...<\/span>\r\nconfig.dressForRain.grabUmbrella = <span style=\"color: #0000FF\">...<\/span>\r\n<span style=\"color: #0000FF\">...<\/span>\r\nconfig.dressForSun.getDressed = {<span style=\"color: #A020F0\">'putOnSequinedJeanJacket'<\/span>\r\n                                 <span style=\"color: #A020F0\">'putOnSneakers'<\/span>};\r\nconfig.dressForSun.putOnSequinedJeanJacket = putOnJacket(<span style=\"color: #A020F0\">'jeanJacket'<\/span>);\r\nconfig.putOnJacket = <span style=\"color: #0000FF\">...<\/span>\r\n<span style=\"color: #0000FF\">...<\/span><\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">if<\/span> isRaining\r\n  outfit = assembleOutfit(config, <span style=\"color: #A020F0\">'dressForRain'<\/span>);\r\n<span style=\"color: #0000FF\">else<\/span>\r\n  outfit = assembleOutfit(config, <span style=\"color: #A020F0\">'dressForSun'<\/span>);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">getDressed(outfit);<\/pre><p>Where the resulting object \"outfit\" is an assembly of unique methods and properties that depends on the state of the weather.\r\n      If it is raining the \"outfit\" object will have a method to \"grabUmbrella\" otherwise the outfit will not have any method to acquire\r\n      an umbrella. Some of the component elements can even be shared, like \"putOnJacket\". It's very flexible, and will work as long\r\n      as you have a method to \"getDressed\".\r\n   <\/p>\r\n   <h3>Why Did I Choose This File Exchange Entry<a name=\"3\"><\/a><\/h3>\r\n   <p>I had never heard of dependency injection prior to coming across this File Exchange entry. While I had actually seen it and\r\n      experienced it many times, I didn't really understand much of what was going on.\r\n   <\/p>\r\n   <p>So I thought I would share my learning experience with you.<\/p>\r\n   <p>The documentation for this entry has some excellent examples of how the framework is used. It also points to some very nice\r\n      articles that describe <a href=\"http:\/\/www.martinfowler.com\/articles\/injection.html\">dependency injection<\/a> better than I can.\r\n   <\/p>\r\n   <h3>Do You Employ Dependency Injection?<a name=\"4\"><\/a><\/h3>\r\n   <p>Let us know if this is something you use. In MATLAB? In other programming languages?<\/p>\r\n   <p>You can submit a response to this post <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6233#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0644783cbde64a928e5edb77fe34f5a8() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='0644783cbde64a928e5edb77fe34f5a8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0644783cbde64a928e5edb77fe34f5a8';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Greg Wolff';\r\n        copyright = 'Copyright 2015 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0644783cbde64a928e5edb77fe34f5a8()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 8.6<br><\/p>\r\n<\/div>\r\n<!--\r\n0644783cbde64a928e5edb77fe34f5a8 ##### SOURCE BEGIN #####\r\n%% Doctor, Do I Need a Dependency Injection?\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620 Greg's> pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52394-matlab-dependency-injection MATLAB Dependency Injection>\r\n% by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6581434-matt-mcdonnell Matt McDonnell>.\r\n% \r\n% Imagine you want to build an application that dresses you in the morning.\r\n% \r\n% What you wear on any given day will depend on the weather that day. If it's raining, you might wear a rain slicker, rain boots, and carry an umbrella.  If it's bright and sunny you might not wear a jacket at all, wear sneakers, and ditch the umbrella.\r\n% \r\n% How do you build an application that can perform the right operations with a large number of different scenarios.\r\n% \r\n% One methodology you can apply is dependency injection.\r\n% \r\n%% Dependency Whaaaaa?\r\n% |\"Dependency injection is a technique for constructing an application\r\n% from components without the individual components needing to be coupled to a particular implementation of other components.\"|\r\n% \r\n% I stole this right out of the top of the documentation for the \"demo_mdepin\" function in the File Exchange entry.\r\n% \r\n% _*But wait... what does that even mean?*_\r\n% \r\n% Basically this refers to the notion of software plugins.  You have a generic application code that is customized by applying different specific implementations.\r\n% \r\n% This enables you to configure the application based on selection criteria like, is it raining.\r\n% \r\n% The modular and independent nature of the components also enable more simplified testing strategies.\r\n% \r\n%% You've Seen This Somewhere Before\r\n% You might be thinking \"hmmm, this seems like using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/function-handles.html function\r\n% handles> in MATLAB.\"\r\n% \r\n%   if isRaining\r\n%     coatFunction = @putOnRainSlicker;\r\n%   else\r\n%     coatFunction = @putOnSequinedJeanJacket;\r\n%   end\r\n% \r\n%   getDressed(coatFunction);\r\n% \r\n% Dependency injection itself is more than just passing around handles to different functions. However the above illustrates the key concept that you can vary implementation by determining which function gets called through some configuration.\r\n% \r\n% But you're not just changing which coat you are wearing when it is raining. You might have completely different clothing needs depending on the weather.\r\n% \r\n% It's more likely you would do something like:\r\n% \r\n%   config = struct();\r\n%   config.dressForRain.getDressed = {'putOnRainSlicker'\r\n%                                     'putOnRainBoots'\r\n%                                     'grabUmbrella'};\r\n%   config.dressForRain.putOnRainSlicker = putOnJacket('rainSlicker');\r\n%   config.dressForRain.putOnRainBoots = ...\r\n%   config.dressForRain.grabUmbrella = ...\r\n%   ...  \r\n%   config.dressForSun.getDressed = {'putOnSequinedJeanJacket'\r\n%                                    'putOnSneakers'};\r\n%   config.dressForSun.putOnSequinedJeanJacket = putOnJacket('jeanJacket');\r\n%   config.putOnJacket = ...\r\n%   ...\r\n% \r\n%   if isRaining\r\n%     outfit = assembleOutfit(config, 'dressForRain');\r\n%   else\r\n%     outfit = assembleOutfit(config, 'dressForSun');\r\n%   end\r\n%   \r\n%   getDressed(outfit);\r\n% \r\n% Where the resulting object \"outfit\" is an assembly of unique methods and properties that depends on the state of the weather.  \r\n% If it is raining the \"outfit\" object will have a method to \"grabUmbrella\"\r\n% otherwise the outfit will not have any method to acquire an umbrella.\r\n% Some of the component elements can even be shared, like \"putOnJacket\".\r\n% It's very flexible, and will work as long as you have a method to \"getDressed\".\r\n\r\n%% Why Did I Choose This File Exchange Entry\r\n% I had never heard of dependency injection prior to coming across this File Exchange entry. While I had actually seen it and experienced it many times, I didn't really understand much of what was going on.\r\n% \r\n% So I thought I would share my learning experience with you.\r\n% \r\n% The documentation for this entry has some excellent examples of how the framework is used.\r\n% It also points to some very nice articles that describe <http:\/\/www.martinfowler.com\/articles\/injection.html dependency injection> better than I can.\r\n% \r\n%% Do You Employ Dependency Injection?\r\n% Let us know if this is something you use. In MATLAB? In other programming languages?\r\n% \r\n% You can submit a response to this post <https:\/\/blogs.mathworks.com\/pick\/ respond here>.<code>\r\n\r\n##### SOURCE END ##### 0644783cbde64a928e5edb77fe34f5a8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Greg's pick this week is MATLAB Dependency Injection by Matt McDonnell.\r\n      \r\n      Imagine you want to build an application that dresses you in the morning.\r\n     ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/10\/23\/doctor-do-i-need-a-dependency-injection\/\">read more >><\/a><\/p>","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6233"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6233"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6233\/revisions"}],"predecessor-version":[{"id":8807,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6233\/revisions\/8807"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}