This file is indexed.

/usr/share/doc/libneedle-ruby1.8/doc/manual-html/chapter-3.html is in libneedle-ruby1.8 1.3.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<html>
  <head>
    <title>Needle Manual :: Chapter 3: Service Locator</title>
    <link type="text/css" rel="stylesheet" href="stylesheets/manual.css" />
  </head>
  
  <body>
    <div id="banner">
      <table border='0' cellpadding='0' cellspacing='0' width='100%'>
        <tr><td valign='top' align='left'>
          <div class="title">
            <span class="product">Needle&mdash;</span><br />
            <span class="tagline">to the point --></span>
          </div>
        </td><td valign='middle' align='right'>
          <div class="info">
            Needle Version: <strong>1.3.0</strong><br />
            Manual Last Updated: <strong>2005-12-24 15:20 UTC</strong>
          </div>
        </td></tr>
      </table>
    </div>

    <table border='0' width='100%' cellpadding='0' cellspacing='0'>
      <tr><td valign='top'>

        <div id="navigation">
          <h1>Needle Manual</h1>

          <h2>Chapters</h2>
          <ol type="I">
          
            <li>
                <a href="chapter-1.html">
                Introduction
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-1.html#s1">What is Needle?</a></li>
                
                  <li><a href="chapter-1.html#s2">How Can It Help Me?</a></li>
                
                  <li><a href="chapter-1.html#s3">Alternatives</a></li>
                
                  <li><a href="chapter-1.html#s4">License Information</a></li>
                
                  <li><a href="chapter-1.html#s5">Support</a></li>
                
              </ol>
            </li>
          
            <li>
                <a href="chapter-2.html">
                Registry
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-2.html#s1">Overview</a></li>
                
                  <li><a href="chapter-2.html#s2">Creating</a></li>
                
                  <li><a href="chapter-2.html#s3">Services</a></li>
                
                  <li><a href="chapter-2.html#s4">Namespaces</a></li>
                
              </ol>
            </li>
          
            <li><strong>
                <a href="chapter-3.html">
                Service Locator
                </a>
                </strong> <big>&larr;</big>
              <ol type="1">
                
                  <li><a href="chapter-3.html#s1">Overview</a></li>
                
                  <li><a href="chapter-3.html#s2">Conventional Architecture</a></li>
                
                  <li><a href="chapter-3.html#s3">Locator Pattern</a></li>
                
              </ol>
            </li>
          
            <li>
                <a href="chapter-4.html">
                Dependency Injection
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-4.html#s1">Overview</a></li>
                
                  <li><a href="chapter-4.html#s2">Setup</a></li>
                
              </ol>
            </li>
          
            <li>
                <a href="chapter-5.html">
                Interceptors
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-5.html#s1">Overview</a></li>
                
                  <li><a href="chapter-5.html#s2">Architecture</a></li>
                
                  <li><a href="chapter-5.html#s3">Attaching</a></li>
                
                  <li><a href="chapter-5.html#s4">Ordering</a></li>
                
                  <li><a href="chapter-5.html#s5">Custom</a></li>
                
              </ol>
            </li>
          
            <li>
                <a href="chapter-6.html">
                Service Models
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-6.html#s1">Overview</a></li>
                
                  <li><a href="chapter-6.html#s2">Pipelines</a></li>
                
                  <li><a href="chapter-6.html#s3">Models</a></li>
                
              </ol>
            </li>
          
            <li>
                <a href="chapter-7.html">
                Logging
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-7.html#s1">Overview</a></li>
                
                  <li><a href="chapter-7.html#s2">LogFactory</a></li>
                
                  <li><a href="chapter-7.html#s3">Configuration</a></li>
                
              </ol>
            </li>
          
            <li>
                <a href="chapter-8.html">
                Service Libraries
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-8.html#s1">Overview</a></li>
                
                  <li><a href="chapter-8.html#s2">Creating Libraries</a></li>
                
                  <li><a href="chapter-8.html#s3">Using Libraries</a></li>
                
              </ol>
            </li>
          
            <li>
                <a href="chapter-9.html">
                Customizing Needle
                </a>
                
              <ol type="1">
                
                  <li><a href="chapter-9.html#s1">Namespaces</a></li>
                
                  <li><a href="chapter-9.html#s2">Interceptors</a></li>
                
                  <li><a href="chapter-9.html#s3">Contexts</a></li>
                
              </ol>
            </li>
          
          </ol>

          <h2>Other Documentation</h2>

          <ul>
            <li><a href="http://needle.rubyforge.org/api/index.html">Needle API</a></li>
            <li><a href="http://needle.rubyforge.org/faq.html">Needle FAQ</a></li>
          </ul>

          <h2>Tutorials</h2>
          <ol>
          
          </ol>

          <p align="center"><strong>More To Come...</strong></p>

          <div class="license">
            <a href="http://creativecommons.org/licenses/by-sa/2.0/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights" /></a><br />
            This manual is licensed under a <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons License</a>.
          </div>
        </div>

      </td><td valign='top' width="100%">

        <div id="content">

           <div class="top"><div class="prevnext">
  
    <a href="chapter-2.html">Previous (2. Registry)</a> |
  
  <a href="index.html">Up</a>
  
    | <a href="chapter-4.html">Next (4. Dependency Injection)</a>
  
</div></div>

<h1>3. Service Locator</h1>



     <h2>
       <a name="s1"></a>
       3.1. Overview
     </h2>

   

   <div class="section">
     <p>The <em>service locator</em> design pattern can be considered a subset of dependency injection. Because it is simpler, it is as good of a place to start teaching DI as any.</p>


	<p>To demonstrate both techniques, we&#8217;ll pretend we&#8217;re going to write an online forum application. To start, let&#8217;s come up with a rough design by cataloging the components we&#8217;ll need.</p>


	<ul>
	<li><code>Logger</code>. This will be used to write messages to a file.</li>
		<li><code>Authenticator</code>. This will be used to validate whether a user is who they say they are.</li>
		<li><code>Database</code>. This encapsulates access to the database that will store our forum data.</li>
		<li><code>Session</code>. This represents a single user&#8217;s session.</li>
		<li><code>View</code>. The presentation manager, used to render pages to the user.</li>
		<li><code>Application</code>. The controller that ties it all together.</li>
	</ul>


	<p>(Of course, a <em>real</em> online forum application would be significantly more complex, but the above components will do for our purposes.)</p>


	<p>The dependencies between these components are:</p>


	<ul>
	<li><code>Authenticator</code> <em>has</em> <code>Database</code> (for querying user authentication information) and <code>Logger</code></li>
		<li><code>Database</code> <em>has</em> <code>Logger</code> (for indicating database accesses and query times)</li>
		<li><code>Session</code> <em>has</em> <code>Database</code> (for storing session information) and <code>Logger</code></li>
		<li><code>Application</code> <em>has</em> <code>Database</code>, <code>View</code>, <code>Session</code>, and <code>Authenticator</code>, and <code>Logger</code></li>
	</ul>
   </div>



     <h2>
       <a name="s2"></a>
       3.2. Conventional Architecture
     </h2>

   

   <div class="section">
     <p>A conventional architecture will have each component instantiate its own dependencies. For example, the <code>Application</code> would do something like this:</p>


	<div class='figure'>
<span class='caption'>A conventional application implementation [ruby]</span>
<div class='body'><table border='0' cellpadding='0' cellspacing='0'><tr><td class='lineno'>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></td><td width='100%'><link rel='stylesheet' type='text/css' href='stylesheets/ruby.css' /><div class='ruby'><pre><span class="keyword">class </span><span class="class">Application</span>
  <span class="keyword">def </span><span class="method">initialize</span>
    <span class="attribute">@logger</span> <span class="punct">=</span> <span class="constant">Logger</span><span class="punct">.</span><span class="ident">new</span>
    <span class="attribute">@authenticator</span> <span class="punct">=</span> <span class="constant">Authenticator</span><span class="punct">.</span><span class="ident">new</span>
    <span class="attribute">@database</span> <span class="punct">=</span> <span class="constant">Database</span><span class="punct">.</span><span class="ident">new</span>
    <span class="attribute">@view</span> <span class="punct">=</span> <span class="constant">View</span><span class="punct">.</span><span class="ident">new</span>
    <span class="attribute">@session</span> <span class="punct">=</span> <span class="constant">Session</span><span class="punct">.</span><span class="ident">new</span>
  <span class="keyword">end</span>
<span class="keyword">end</span></pre></div></td></tr></table></div></div>


	<p>However, the above is already flawed, because the <code>Authenticator</code> and the <code>Session</code> both need access to the <code>Database</code>, so you really need to make sure you instantiate things in the right order and pass them as parameters to the constructor of each object that needs them, like so:</p>


	<div class='figure'>
<span class='caption'>A parameterized application implementation [ruby]</span>
<div class='body'><table border='0' cellpadding='0' cellspacing='0'><tr><td class='lineno'>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></td><td width='100%'><link rel='stylesheet' type='text/css' href='stylesheets/ruby.css' /><div class='ruby'><pre><span class="keyword">class </span><span class="class">Application</span>
  <span class="keyword">def </span><span class="method">initialize</span>
    <span class="attribute">@view</span> <span class="punct">=</span> <span class="constant">View</span><span class="punct">.</span><span class="ident">new</span>
    <span class="attribute">@logger</span> <span class="punct">=</span> <span class="constant">Logger</span><span class="punct">.</span><span class="ident">new</span>
    <span class="attribute">@database</span> <span class="punct">=</span> <span class="constant">Database</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span> <span class="attribute">@logger</span> <span class="punct">)</span>
    <span class="attribute">@authenticator</span> <span class="punct">=</span> <span class="constant">Authenticator</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span> <span class="attribute">@logger</span><span class="punct">,</span> <span class="attribute">@database</span> <span class="punct">)</span>
    <span class="attribute">@session</span> <span class="punct">=</span> <span class="constant">Session</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span> <span class="attribute">@logger</span><span class="punct">,</span> <span class="attribute">@database</span> <span class="punct">)</span>
  <span class="keyword">end</span>
<span class="keyword">end</span></pre></div></td></tr></table></div></div>


	<p>The problem with this is that if you later decide that <code>View</code> needs to access the database, you need to rearrange the order of how things are instantiated in the <code>Application</code> constructor.</p>
   </div>



     <h2>
       <a name="s3"></a>
       3.3. Locator Pattern
     </h2>

   

   <div class="section">
     <p>The <em>service locator</em> pattern makes things a <em>little</em> easier. Instead of instantiating everything in the constructor of the <code>Application</code>, you can create a factory method somewhere that returns the new <code>Application</code> instance. Then, inside of this factory method, you assign each new object to collection, and pass that collection to each constructor.</p>


	<div class='figure'>
<span class='caption'>Service locator example [ruby]</span>
<div class='body'><table border='0' cellpadding='0' cellspacing='0'><tr><td class='lineno'>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></td><td width='100%'><link rel='stylesheet' type='text/css' href='stylesheets/ruby.css' /><div class='ruby'><pre><span class="ident">require</span> <span class="punct">'</span><span class="string">needle</span><span class="punct">'</span>

<span class="keyword">def </span><span class="method">create_application</span>
  <span class="ident">locator</span> <span class="punct">=</span> <span class="constant">Needle</span><span class="punct">::</span><span class="constant">Registry</span><span class="punct">.</span><span class="ident">new</span>

  <span class="ident">locator</span><span class="punct">.</span><span class="ident">register</span><span class="punct">(</span> <span class="symbol">:view</span> <span class="punct">)</span> <span class="punct">{</span> <span class="constant">View</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
  <span class="ident">locator</span><span class="punct">.</span><span class="ident">register</span><span class="punct">(</span> <span class="symbol">:logger</span> <span class="punct">)</span> <span class="punct">{</span> <span class="constant">Logger</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
  <span class="ident">locator</span><span class="punct">.</span><span class="ident">register</span><span class="punct">(</span> <span class="symbol">:database</span> <span class="punct">)</span> <span class="punct">{</span> <span class="constant">Database</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
  <span class="ident">locator</span><span class="punct">.</span><span class="ident">register</span><span class="punct">(</span> <span class="symbol">:authenticator</span> <span class="punct">)</span> <span class="punct">{</span><span class="constant">Authenticator</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
  <span class="ident">locator</span><span class="punct">.</span><span class="ident">register</span><span class="punct">(</span> <span class="symbol">:session</span> <span class="punct">)</span> <span class="punct">{</span> <span class="constant">Session</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
  <span class="ident">locator</span><span class="punct">.</span><span class="ident">register</span><span class="punct">(</span> <span class="symbol">:app</span> <span class="punct">)</span> <span class="punct">{</span> <span class="constant">Application</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>

  <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:app</span><span class="punct">]</span>
<span class="keyword">end</span>

<span class="keyword">class </span><span class="class">Application</span>
  <span class="keyword">def </span><span class="method">initialize</span><span class="punct">(</span> <span class="ident">locator</span> <span class="punct">)</span>
    <span class="attribute">@view</span> <span class="punct">=</span> <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:view</span><span class="punct">]</span>
    <span class="attribute">@logger</span> <span class="punct">=</span> <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:logger</span><span class="punct">]</span>
    <span class="attribute">@database</span> <span class="punct">=</span> <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:database</span><span class="punct">]</span>
    <span class="attribute">@authenticator</span> <span class="punct">=</span> <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:authenticator</span><span class="punct">]</span>
    <span class="attribute">@session</span> <span class="punct">=</span> <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:session</span><span class="punct">]</span>
  <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="keyword">class </span><span class="class">Session</span>
  <span class="keyword">def </span><span class="method">initialize</span><span class="punct">(</span> <span class="ident">locator</span> <span class="punct">)</span>
    <span class="attribute">@database</span> <span class="punct">=</span> <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:database</span><span class="punct">]</span>
    <span class="attribute">@logger</span> <span class="punct">=</span> <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:logger</span><span class="punct">]</span>
  <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="punct">...</span></pre></div></td></tr></table></div></div>


	<p>This has the benefit of allowing each object to construct itself <em>รก la carte</em> from the objects in the locator. Also, each object no longer cares what class implements each service&#8212;it only cares that each object implements the methods it will attempt to invoke on that object.</p>


	<p>Also, because Needle defers the instantiation of each service until the service is actually requested, we can actually register each item with the locator in any arbitrary order. All that is happening is the block is associated with the symbol, so that when the service is requested, the corresponding block is invoked. What is more, by default each service is then cached, so that it is only instantiated once.</p>


	<p>Thus, when we get the <code>:app</code> service (on the last line), the <code>Application</code> constructor is invoked, passing the locator to the constructor. Inside the constructor, <code>Application</code> retrieves each of its dependencies from the locator, causing each of them to be instantiated in turn. By this means, everything is initialized and constructed when the <code>create_application</code> method returns.</p>


	<p>In the interest of brevity, the <code>create_application</code> could have been written like this, using a &#8220;builder&#8221; object (called <code>b</code> in the example below) to help register the services:</p>


	<div class='figure'>
<span class='caption'>Service locator example using #define [ruby]</span>
<div class='body'><table border='0' cellpadding='0' cellspacing='0'><tr><td class='lineno'>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></td><td width='100%'><link rel='stylesheet' type='text/css' href='stylesheets/ruby.css' /><div class='ruby'><pre><span class="keyword">def </span><span class="method">create_application</span>
  <span class="ident">locator</span> <span class="punct">=</span> <span class="constant">Needle</span><span class="punct">::</span><span class="constant">Registry</span><span class="punct">.</span><span class="ident">define</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">b</span><span class="punct">|</span>
    <span class="ident">b</span><span class="punct">.</span><span class="ident">view</span> <span class="punct">{</span> <span class="constant">View</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
    <span class="ident">b</span><span class="punct">.</span><span class="ident">logger</span> <span class="punct">{</span> <span class="constant">Logger</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
    <span class="ident">b</span><span class="punct">.</span><span class="ident">database</span> <span class="punct">{</span> <span class="constant">Database</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
    <span class="ident">b</span><span class="punct">.</span><span class="ident">authenticator</span> <span class="punct">{</span><span class="constant">Authenticator</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
    <span class="ident">b</span><span class="punct">.</span><span class="ident">session</span> <span class="punct">{</span> <span class="constant">Session</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
    <span class="ident">b</span><span class="punct">.</span><span class="ident">app</span> <span class="punct">{</span> <span class="constant">Application</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">locator</span><span class="punct">)</span> <span class="punct">}</span>
  <span class="keyword">end</span>

  <span class="ident">locator</span><span class="punct">[</span><span class="symbol">:app</span><span class="punct">]</span>
<span class="keyword">end</span></pre></div></td></tr></table></div></div>
   </div>



<div class="bottom"><div class="prevnext">
  
    <a href="chapter-2.html">Previous (2. Registry)</a> |
  
  <a href="index.html">Up</a>
  
    | <a href="chapter-4.html">Next (4. Dependency Injection)</a>
  
</div></div>


        </div>

      </td></tr>
    </table>
  </body>
</html>