{"id":4559,"date":"2014-03-30T13:32:08","date_gmt":"2014-03-30T13:32:08","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/abstract-factories-for-injecting-runtime-dependencies-in-complex-hierarchies-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:32:08","modified_gmt":"2014-03-30T13:32:08","slug":"abstract-factories-for-injecting-runtime-dependencies-in-complex-hierarchies-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/abstract-factories-for-injecting-runtime-dependencies-in-complex-hierarchies-collection-of-common-programming-errors\/","title":{"rendered":"Abstract factories for injecting runtime dependencies in complex hierarchies-Collection of common programming errors"},"content":{"rendered":"<p>Given the following class hierarchy<\/p>\n<ul>\n<li>ClassA needs ClassB<\/li>\n<li>ClassB needs ClassC<\/li>\n<\/ul>\n<p>we get a dependency graph like this:<\/p>\n<pre><code>ClassA --&gt; ClassB --&gt; ClassC\n<\/code><\/pre>\n<p>so if we use DI we inject ClassC into ClassB and ClassB into ClassA.<\/p>\n<p>But now let&#8217;s say ClassC is a runtime dependency (for example some kind of strategy). The proposed way to inject a runtime dependency is to introduce an abstract factory like a<\/p>\n<pre><code>ClassCFactory\n<\/code><\/pre>\n<p>Now we can inject ClassCFactory into ClassB and get the following graph<\/p>\n<pre><code>ClassA --&gt; ClassB --&gt; ClassCFactory\n<\/code><\/pre>\n<p>Now we have a method in ClassB which we can call to let the factory do its work. For example<\/p>\n<pre><code>ObjB.SelectC(MyRuntimeValue)\n<\/code><\/pre>\n<p>But now in our application we do not know anything about ClassB (perhaps there are some more layers involved). One solution might be to have a SelectC in ClassA<\/p>\n<pre><code>ObjA.SelectC(MyRuntimeValue) -(calls)-&gt; ObjB.SelectC(MyRuntimeValue)\n<\/code><\/pre>\n<p>or we simply violate the Law Of Demeter and do something like<\/p>\n<pre><code>ObjA.ObjB.SelectC(MyRuntimeValue) \n<\/code><\/pre>\n<p>I think everyone agrees that the second solution is not the way to go. But the first solution also has several drawbacks, especially if we have more layers in between.<\/p>\n<p>We could also pull up the factory one level to create ClassB, but is ClassB really a runtime dependency? What solutions do you suggest? Or is it even a bad class design?<\/p>\n<p>IMHO it is always better to have a dependency on what the object actually needs to do its work and not on a factory which creates the object needed. But with this thought in mind a DI container would be useless&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the following class hierarchy ClassA needs ClassB ClassB needs ClassC we get a dependency graph like this: ClassA &#8211;&gt; ClassB &#8211;&gt; ClassC so if we use DI we inject ClassC into ClassB and ClassB into ClassA. But now let&#8217;s say ClassC is a runtime dependency (for example some kind of strategy). The proposed way [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4559","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4559"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4559\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}