2

I'm just beginning to learn about Duck-typing, and Inversion of Control.

In practical real world examples why would I want to incorporate these concepts into my code?

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
CodingIsAwesome
  • 1,946
  • 7
  • 36
  • 54
  • 1
    IoC is marketed as "make a component, let the user decide how to plug it in" type of deal, isn't it? –  Apr 18 '11 at 04:22
  • Yep, but I'm usually not developing code for other developers. Would I use this if I was designing an API or something? – CodingIsAwesome Apr 18 '11 at 04:24
  • Um... I'm imagining a scenario where the same code will provide a service to different kinds of clients, like a smartphone app, a web site, and a desktop application... you write it once and incorporate it three times. sounds like a big project for one guy. –  Apr 18 '11 at 04:26
  • In my experience, designing interface first makes you come up with covering complete functionality. – Yeameen Apr 18 '11 at 04:49
  • Aflac quacks too, but it is not a duck. – irreputable Apr 18 '11 at 16:17

5 Answers5

6

There are many benefits of coding to an Interface, even if you do not "develop code for other developers" as hinted in your comment. Discussing these benefits is a worthy exercise, but since the question asks for real world examples/use cases, here goes:

  • For testing.   IoC makes it easy to introduce mocks and other simplified/test classes which allow testing particular units without having to bring in all the dependencies of the project into play.
  • For implementing particular portions of the application before some dependencies are available. (similar to the testing case)
  • To deliver different sets of functionality to different users. You can have multiple "editions" of a given module, and deliver the appropriate module to different types of customers: Evaluation edition, Standard Edition, Advanced Edition and even Beta Test Edition. So long as the API of the module is respected, the rest of the application works the same without having to worry about the particular nature of the module in use.
  • To use particular frameworks. This example may be perceived as a circular reference: "Why do these frameworks use IoC in the first place?", but the fact is that the IoC pattern is omnipresent in several libraries and frameworks which are in of themselves very useful.
  • To create interceptors. While interceptor are a useful debug device, they can also be used for so many purposes.

Note how I tried and provide only examples with a very clear use case. Be aware that there are many other reasons to use IoC and related concepts (for example to help with producing a modular design, to improve readability...) but such reasons/advantages are more subtle, less objective, but none less maybe just as important!

mjv
  • 73,152
  • 14
  • 113
  • 156
  • +1 - slightly cynical but also true :) – ta.speot.is Apr 18 '11 at 04:31
  • 1
    lol yes +1 here made me laugh – CodingIsAwesome Apr 18 '11 at 04:32
  • Man, you turned it into a serious reply. A great joke died today. –  Apr 18 '11 at 04:50
  • @bdares and others. Yes, the joke wasn't lost on me... I had to deal with a local distraction and hence cut my response short initially. The humor wasn't intentional though I had a big LOL when comments pointed this out. I figured I should complete the answer, for IoC deserves a better treatment. – mjv Apr 18 '11 at 05:00
2

As I understand it Dependency Injection is a design pattern which is implemented by Inversion of Control which is closer to principle in software design.

One example of Dependency Injection could be the separation of Car and Engine into two separate classes. The car needs an engine to function and will run with any engine. The engine is then Dependency Injected into the car.

A benefit of IoC/Dependency is clear separation of classes and their uses. A class can be replaced by another without braking your code. Dependency Injection could be done at runtime which allows for easy re-configuration of your software.

Filip
  • 656
  • 4
  • 8
1

One recent experience taught me how easy live becomes when using IoC. In a recent work, I had to clean a user submitted html. The same cleaner service is used in many places in the project. But previous regex cleaner isn't working for some particular cases. I just wrote a new implementation with JSoup library and linked where it is needed. The switching become so simple with Spring IoC :)

Yeameen
  • 833
  • 7
  • 8
0

Quoting this article on the subject of IoC:

Using object-oriented design principles and features such as interface, inheritance, and polymorphism, the IoC pattern enables better software design that facilitates reuse, loose coupling, and easy testing of software components.

(emphasis mine)

These sound like great reasons to me. Are you looking for more?

As for Duck Typing, the jury on that is out still, IMO. It has some nice benefits in some circumstances, but leads to more runtime error possibility.

JUST MY correct OPINION
  • 35,674
  • 17
  • 77
  • 99
  • Ok ok yes, those are all very good reasons, but my boss could give a crap less about how creative my code is. Quite unfortunate. – CodingIsAwesome Apr 18 '11 at 04:38
  • This has nothing to do with creativity and everything to do with measurable improvements across the board. Facilitating reuse means you don't spend the time, money, etc. to reimplement almost-but-not-quite the same code over and over again (+ testing costs). Loose coupling means if your requirements change you don't have to change everything else (and also means making new stuff is easier). Easy testing means your testing costs go down. There's NOTHING in there about creativity and EVERYTHING to do with improved software quality at lower development prices. – JUST MY correct OPINION Apr 18 '11 at 04:50
0

IoC basically help you decouple instantiation from your code at dev time. So the runtime objects can be injected (instead of your code instantiating them) by some outside agency called container - as long as they obey the interface. Program to Interface is the first principle in the design pattern book. And the advantages are great. In IoC, it helps in decoupling your code.

Dependency Injection and Dependency Pull are two different forms of IoC.

Have a look at thislink

A side point - in java world Spring framework even allows introductions of new methods.

Not sure about the DuckTyping.

Atul
  • 2,673
  • 2
  • 28
  • 34