Page 2 - 10.1.1.135.5516
P. 2

our analysis techniques. In section 4 we present the results  ‘b’ispassedin asa parameter in A’s constructor. We will,
           of our study and discuss our interpretation of these results  for the moment, avoid a discussion of whether B should be
           in section 5. Section 6 then presents our conclusions.  an interface type, abstract type or concrete type. The key
                                                               observation is that A does not depend on a particular im-
           2 Background                                        plementation of B. Particularly, when clients instantiate A,
                                                               they get to specify the particular subtype of B to be assigned
                                                               to ‘b’ at runtime. This can have beneficial consequences to
             The phrase Dependency Inversion Principle was first
                                                               several software design quality attributes.
           coined by Martin in 1996 [16] although the concept it repre-
           sents has been discussed by many others under the guise of
           different names. Fowler [6] dates the concept back to John-
           son and Foote’s discussion of Inversion of Control (IOC)  2.1  Effects on Quality
           [12] in 1988, and he notes that Sweet also alludes to it
           in 1985 with the more “colourful” phrase the Hollywood’s
           Law [27]. Lakos [14, ch.6] also discusses the DIP under the
           guise of insulation.
                                                                 It has been argued that Dependency Injection affects
             While the DIP is easily stated at a conceptual level, defin-
                                                               many quality attributes, in particular extensibility, testabil-
           ing it concretely, in terms of entities in Java source code,
                                                               ity, and reusability.
           is less straightforward. A conceptual statement of the DIP
           is that by Martin: “High-level modules should not depend  Extensibility can be defined as “the ease with which a
           upon low-level modules. Both should depend upon abstrac-  system or component can be modified to increase its stor-
           tions” [16]. If we glean the examples given by Martin we  age or functional capacity” [10]. In the above snippet A is
           might take this to mean that in Java a class should depend  arguably more extensible because it can be used with dif-
           on interface or abstract types, not concrete types, although  ferent implementations of B without modifying the source
           there are some benefits that accrue even with concrete types.  code of A. Indeed this is why DI is used at the “plug-points”
             Besides the issue of whether we should depend on inter-  of application frameworks [12, 24].
           face, abstract or concrete types we must deal with the “prob-
           lem of instantiation” [18], or as the Gang of Four state “you  Testability can be defined as “the degree to which a sys-
           have to instantiate concrete classes (that is, specify a partic-  tem or component facilitates the establishment of test cri-
           ular implementation) somewhere in your system” [7, p.18].  teria and the performance of tests to determine whether
           This is another challenge in concretely stating, and measur-  those criteria have been met” [10]. Dependency Injection
           ing the DIP — we need to know the mechanism by which  supports the use of mock objects to help unit test a class
           concrete classes are instantiated and passed in to their DIP  [15, 28]. Mock objects can be used to both provide control
           exhibiting clients.                                 and observe the class under test.
             There are actually many ways to instantiate and pass in
                                                                 Reusability can be defined as “the degree to which a soft-
           concrete classes to those exhibiting the DIP. Fowler identi-
                                                               ware module or other work product can be used in more
           fies the Dependency Injection and Service Locator approach
                                                               than one computer program or software system” [10]. De-
           [5]. In this work, we concentrate on the Dependency Injec-
                                                               pendency injection can improve reuse by (1) improving
           tion form of the DIP.
                                                               flexibility and (2) breaking transitive dependencies.
             In the Dependency Injection (DI) form of the DIP, as it is
           discussed by Fowler [5], the object assigned to the field of  DI can improve flexibility because different implementa-
           a class is passed in through one of that class’ constructors  tions can be used with the class we want to reuse, improving
           or methods. A simple illustration of dependency injection  the degree to which that class can be used in multiple situa-
           is as follows:                                      tions, as discussed discussed above in extensibility. Depen-
                                                               dency injection can reduce the number of classes we have
           class A {
                                                               to deploy in the context of a new system by breaking transi-
             Bb;
                                                               tive dependencies. This is important because to effectively
             public A(B b) {
                                                               reuse a class it should not be tied to a large block of un-
               this.b = b;
                                                               necessary code [14, p.14]. If a class we reuse depends on a
             }
                                                               class type, from the perspective of reuse, it also transitively
             //...
           }                                                   depends on any types that appear in the private part of that
                                                               class type. If the type it depends on is an interface type then
             In the above code example A is exhibiting dependency  it does not transitively depend on any private parts of that
           injection because the object that gets assigned to its field  interface’s implementation.
   1   2   3   4   5   6   7