{"id":3284,"date":"2019-10-16T10:48:33","date_gmt":"2019-10-16T10:48:33","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=3284"},"modified":"2024-02-16T10:03:22","modified_gmt":"2024-02-16T10:03:22","slug":"dependency-injection-design-pattern","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=3284","title":{"rendered":"Dependency injection (Design pattern)"},"content":{"rendered":"<p>As the name of this creational design pattern already shows dependencies are getting injected (as parameter) instead of creating them internally.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nclass IAudioControl {};\r\nclass AudioControl : public IAudioControl {};\r\n \r\nclass OfflineMediaPlayer\r\n{\r\npublic:\r\n  \/\/ Constructor without dependency injection\r\n  OfflineMediaPlayer() { m_AudioControl = new AudioControl(); }\r\n  ~OfflineMediaPlayer() { delete m_AudioControl; }\r\nprivate:\r\n  IAudioControl* m_AudioControl;\r\n};\r\n\r\nclass OnlineMediaPlayer\r\n{\r\npublic:\r\n  \/\/ Constructor with dependency injection\r\n  OnlineMediaPlayer(IAudioControl* audioControl)\r\n    : m_AudioControl(audioControl) {}\r\nprivate:\r\n  IAudioControl* m_AudioControl;\r\n};\r\n\r\nint main()\r\n{\r\n  {\r\n    OfflineMediaPlayer offlineMediaPlayer;\r\n  }\r\n\r\n  {\r\n    AudioControl audioControl;\r\n    OnlineMediaPlayer onlineMediaPlayer(&amp;audioControl);\r\n  }\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As the name of this creational design pattern already shows dependencies are getting injected (as parameter) instead of creating them<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[30],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3284"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3284"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3284\/revisions"}],"predecessor-version":[{"id":16696,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3284\/revisions\/16696"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3284"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}