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 11: Line 11:
     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.
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.
<pre>
class Bike
    def to_s
        "bike"
    end
    def own_it?
        return true
    end
end
</pre>

Revision as of 12:22, 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.

    def remark
        if @thing.own_it?
            puts "I've got a #{@thing}."
        else
            puts "I know a #{@thing}."
        end
    end

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.

class Bike
    def to_s
        "bike"
    end
    def own_it?
        return true
    end
end