CSC/ECE 517 Fall 2010/ch5 5e mf

From Expertiza_Wiki
Revision as of 12:20, 3 November 2010 by Mafashin (talk | contribs)
Jump to navigation Jump to search

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.