CSC/ECE 517 Fall 2010/ch5 5e mf: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=The Dependency Injection Pattern= | =The Dependency Injection Pattern= | ||
The dependency injection pattern is a design pattern for fully decoupling one class from the instantiation of another class | 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> | |||
def remark | |||
if @thing.own_it? | |||
puts "I've got a #{@thing}." | |||
else | |||
puts "I know a #{@thing}." | |||
end | |||
end | |||
</pre> | |||
With this method, Syd can make a statement about any object thing that implements the methods own_it? and to_s. |
Revision as of 12:20, 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.