Realisticly why would I use duck-typing or inversion of control? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
开发者_Python百科Closed 9 years ago.
Improve this questionI'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:
- 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!
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.
精彩评论