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?
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?
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:
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!
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.
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 :)
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.
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.