The PEAK Developers' Center   InversionOfControl UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
The following 275 words could not be found in the dictionary of 50 words (including 50 LocalSpellingWords) and are highlighted below:
And   Another   Applications   As   But   Commentary   Composing   Container   Containers   Control   Critiques   Database   Date   Dependency   Differentiators   Discovery   Discussion   Eggs   Even   Explains   External   First   For   Fowler   Hierarchies   However   Ideally   In   Injection   Instead   Inversion   Io   Java   Kimbro   Lion   Locator   Martin   None   Or   Peak   Perhaps   Pico   Principle   Rather   Second   Service   Setter   Something   Spring   T05   That   The   There   Things   This   Time   Traditionally   What   When   With   about   above   against   aka   all   alternative   an   and   another   approach   are   aren   around   article   articles   bad   bar   based   be   becomes   been   behind   being   below   better   between   binding   breaking   by   called   can   care   chance   choice   choices   class   clear   coded   com   compares   complex   component   components   conform   control   create   created   creates   creating   decision   decisions   def   demanding   dependence   dependencies   dependency   described   details   did   dip   direction   discussions   do   does   don   environment   even   example   external   facet   far   fetch   fewer   flip   focuses   for   frameworks   from   gain   general   given   hard   has   have   held   here   high   hold   id   implementation   in   include   init   injection   inside   instead   interfaces   internally   invert   inverted   is   isn   it   its   jsp   just   know   known   language   less   level   like   likely   low   makes   making   martinfowler   maybe   me   modules   more   mortal   my   name   near   necessarily   needs   no   not   note   now   object   objectmentor   objects   of   older   on   or   org   out   outside   own   part   particular   pattern   patterns   pdf   peak   people   perhaps   perspective   phrase   picocontainer   placing   plain   point   principle   prior   probably   provides   rather   rejection   relationship   renames   resources   reusable   reused   see   self   set   setter   several   side   simple   so   specificly   such   sure   taking   talk   tasked   tends   terms   than   that   the   their   them   there   theserverside   thing   things   think   this   thought   thread   to   tradition   unnecessary   using   variant   versa   vice   was   we   what   when   will   with   within   work   working   would   you   your  

Clear message


Inversion of Control (aka IOC) is about breaking dependence between objects by placing interfaces between them. In particular, Inversion of Control focuses on breaking the dependence of high-level objects on their low-level modules.

Even more specificly, we talk here about dependency injection: When an object is created, it is given its dependencies, rather than making them on its own. (Another approach is ?ServiceDiscovery, not described here.)

Traditionally, an object creates the objects it needs. Something like:

    1 class Eggs:
    2 
    3     def __init__(self):
    4         self.my_spam = Spam("foo", "bar")  # Eggs is creating its own Spam.

However, we can invert the control. Instead of creating the object, and perhaps demanding that the environment conform to its choices, we invert control. The environment will fetch your dependencies for you.

    1 class Eggs:
    2     def __init__(self):
    3         self.my_spam = None
    4 
    5     def set_spam(self, spam):
    6         self.my_spam = spam

(This variant of [WWW]dependency injection is called [WWW]setter injection.)

What did we gain by this? There is more control in the environment now. With the environment taking care of more details, there is less a chance that the object will include a hard-coded decision that the environment has to work around. The flip side is that the environment is now tasked with taking care of more details. But when you are working to create complex reusable components, this tends to be the direction of things: The fewer unnecessary decisions that the component makes, the more likely the component can be reused within another environment.

For a simple example of using peak.binding to do this, see PeakDatabaseApplications. ComposingHierarchies provides a more complex example.

For more about "Inversion of Control" aka "Dependency Inversion Principle," see:

Commentary

"Inversion of control" was probably a bad name choice.

First, it's based on a rejection of a prior tradition. As the older tradition becomes less known, people aren't so sure of what's being inverted against. That is, the phrase is mortal.

Second, the relationship between high-level modules and low-level modules isn't necessarily inverted. Rather, there is now an "environment out there," and "me out here." Perhaps it would be better to think in terms of "inside, outside," or even "near, far," rather than "high, low." When dependencies are all held internally, there is a clear high-low relationship: Things that are held are low, things that hold you are high. But when all dependencies are external, there is no such clear high-low relationship. That thing that you thought was below you may now instead just be a facet of the thing that's "above" you. And vice versa. Or maybe not. The point is, you don't know.

?ExternalControl may have been a better name for the general principle behind "Inversion of Control."

-- ?LionKimbro 2005-04-17 06:04:09

 

PythonPowered
EditText of this page (last modified 2007-03-26 18:36:06)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck