Inversion of control applied in Observer pattern

I am trying to fully understand what is meant by inversion of control (IoC) but I have a feeling that I am coming across conflicting definitions on Internet.

Every explanation of inversion of control I found mentions dependency injection (DI). This kind of confuses me as it makes it look like the equal thing.

In my understanding there is something like several types of IoC which is not explicitely mentioned.

From what I understand inversion of control could be when:

1.High-level component invokes methods on abstract types which executes implementation specific code not known at compile time (I still have problem seeing what is "inverted" here. Word delegation seems more apt here - I am not implying Delegator pattern). The implementation objects are created outside the high-level component and either injected during it's construction or using setter methods (point 2) or using Template pattern when they are provided by subclass (I have seen described this pattern as Factory method pattern which only confuses me more).

2.Process of injecting the implementation of abstract types into high-level component is IoC itself - just another form. Here the control is "inverted" to the objects which construct this high-level component or which inject it with dependencies after it has been created. Again word inverted is confusing for me. Dependency injection seems like apt word.

I partly see the inversion of control here in that basically "work" is done by some concrete projects about which the main component knows only abstract type and these objects are created (and thus the concrete work defined) outside main class and just provided. Still I cannot help seeing this as delegating. And when I think about it more it means that the object which manages main (high-level) component, meaning its even higher (mainer:)) component, decides about the concrete implementations so the term inversion cannot refer to low-level components taking over control from high-level components or can it?

Anyway it seems very different from the point 3 .

3.Observer or Callback pattern. When an object asks to be called later when some event happens. This seems like true inversion of control to me. Because observer/callbacks methods are specifically the methods which are not used by observer but some other object controlling the flow.

Is my reasoning correct? Does Observer pattern employ inversion of control as well? Is dependency injection as a form of inversion of control meant as I mentioned in points 1 & 2?


I agree, inversion is a misleading term. Yes DI is a form of IOC. The Observer pattern uses DI, as the Subject is coupled to objects (Observers) that it needs to notify, at run-time, by the Observers registering themselves with the Subject.

From http://en.wikipedia.org/wiki/Inversion_of_control

inversion of control (IoC) is a programming technique in which a dependent object is coupled to the object it needs at run time.

In object-oriented programming, there are several basic techniques to implement inversion of control. These are:

Using a factory pattern
Using a service locator pattern
Using a dependency injection, for example:
    A constructor injection
    Parameter injection
    A setter injection
    An interface injection
Using a contextualized lookup
Using Template method design pattern
Using strategy design pattern
链接地址: http://www.djcxy.com/p/82242.html

上一篇: OOP基础与SOLID之间的区别?

下一篇: 观察者模式中应用的控制反转