CSC/ECE 517 Fall 2010/ch5 5e mf: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
The dependency injection pattern is a design pattern for fully decoupling one class from the instantiation of another class upon which it depends.  In this sense, it is similar to the factory and service locator patterns which are also concerned with object creation.  The difference between these patterns may be demonstrated through example.  Suppose, that Syd has a strategy for making statements about objects illustrated in the following Ruby code.
The dependency injection pattern is a design pattern for fully decoupling one class from the instantiation of another class upon which it depends.  In this sense, it is similar to the factory and service locator patterns which are also concerned with object creation.  The difference between these patterns may be demonstrated through example.  Suppose, that Syd has a strategy for making statements about objects illustrated in the following Ruby code.
<pre>
<pre>
    def remark
# A strategy for remarking on things.
        if @thing.own_it?
class Syd
            puts "I've got a #{@thing}."
  def remark
        else
    if @thing.got_it?
            puts "I know a #{@thing}."
      puts "I've got a #{@thing}."
        end
    else
      puts "I know a #{@thing}."
     end
     end
  end
end
</pre>
</pre>
With this method, Syd can make a statement about any object thing that implements the methods own_it? and to_s.  The Bike class shown below is an example.
With this method, Syd can make a statement about any object thing that implements the methods got_it? and to_s.  The Bike class shown below is an example.
<pre>
<pre>
# An example of a thing upon which to remark.
class Bike
class Bike
    def to_s
  def to_s
        "bike"
    "bike"
    end
  end
    def own_it?
  def got_it?
        return true
    return true
     end
  end
end
</pre>
However, in order for Syd to use his strategy, he must get ahold of a thing.  The simplest way to do this is to instantiate a thing directly.
<pre>
# An example of direct object instantiation.
class Syd
  def initialize
     @thing = Bike.new
  end
end
end
</pre>
</pre>
However, now all that Syd can talk about is his bike.

Revision as of 12:31, 3 November 2010

The Dependency Injection Pattern

The dependency injection pattern is a design pattern for fully decoupling one class from the instantiation of another class upon which it depends. In this sense, it is similar to the factory and service locator patterns which are also concerned with object creation. The difference between these patterns may be demonstrated through example. Suppose, that Syd has a strategy for making statements about objects illustrated in the following Ruby code.

# A strategy for remarking on things.
class Syd
  def remark
    if @thing.got_it?
      puts "I've got a #{@thing}."
    else
      puts "I know a #{@thing}."
    end
  end
end

With this method, Syd can make a statement about any object thing that implements the methods got_it? and to_s. The Bike class shown below is an example.

# An example of a thing upon which to remark.
class Bike
  def to_s
    "bike"
  end
  def got_it?
    return true
  end
end

However, in order for Syd to use his strategy, he must get ahold of a thing. The simplest way to do this is to instantiate a thing directly.

# An example of direct object instantiation.
class Syd
  def initialize
    @thing = Bike.new
  end
end

However, now all that Syd can talk about is his bike.