You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/singleton.markdown
+12-3Lines changed: 12 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -449,9 +449,18 @@ working with it. Before we take the shotgun approach of a singleton
449
449
object with *global* scope, let's consider other ways our codebase can
450
450
get access to an object:
451
451
452
-
***Pass it in.** The simplest solution, and often the best, is to simply pass the
453
-
object you need as an argument to the functions that need it. It's worth
454
-
considering before we discard it as too cumbersome.
452
+
***Pass it in.** The <spanname="di">simplest</span> solution, and often the
453
+
best, is to simply pass the object you need as an argument to the functions
454
+
that need it. It's worth considering before we discard it as too cumbersome.
455
+
456
+
<asidename="di">
457
+
458
+
Some use the term "dependency injection" to refer
459
+
to this. Instead of code reaching *out* and finding its dependencies by calling into something global, the dependencies are pushed *in* to the code that needs it through parameters.
460
+
461
+
Others reserve "dependency injection" for more complex ways of providing dependencies to code.
462
+
463
+
</aside>
455
464
456
465
Consider a function for rendering objects. In order to render, it
457
466
needs access to an object that represents the graphics device and maintains the render state. It's very common to just pass that in to all of the rendering functions, usually as a parameter named something like `context`.
<p><strong>Pass it in.</strong> The simplest solution, and often the best, is to simply pass the
594
-
object you need as an argument to the functions that need it. It’s worth
595
-
considering before we discard it as too cumbersome.</p>
593
+
<p><strong>Pass it in.</strong> The <spanname="di">simplest</span> solution, and often the
594
+
best, is to simply pass the object you need as an argument to the functions
595
+
that need it. It’s worth considering before we discard it as too cumbersome.</p>
596
+
<asidename="di">
597
+
598
+
<p>Some use the term “dependency injection” to refer
599
+
to this. Instead of code reaching <em>out</em> and finding its dependencies by calling into something global, the dependencies are pushed <em>in</em> to the code that needs it through parameters.</p>
600
+
<p>Others reserve “dependency injection” for more complex ways of providing dependencies to code.</p>
601
+
</aside>
602
+
596
603
<p>Consider a function for rendering objects. In order to render, it
597
604
needs access to an object that represents the graphics device and maintains the render state. It’s very common to just pass that in to all of the rendering functions, usually as a parameter named something like <code>context</code>.</p>
598
605
<p>On the other hand, some objects don’t belong in the signature of a
0 commit comments