为什么动态语言不需要IOC容器 [英] Why are IOC containers unnecessary with dynamic languages

查看:108
本文介绍了为什么动态语言不需要IOC容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在放牧法典"播客第68号上, http://herdingcode.com/herding-code-68-new-year-shenanigans/指出,IOC容器在Python或Javascript或类似的词中没有位置.我假设这是常规知识,并且适用于所有动态语言.为什么?动态语言使IOC容器不必要吗?

Someone on the Herding Code podcast No. 68, http://herdingcode.com/herding-code-68-new-year-shenanigans/, stated that IOC containers had no place with Python or Javascript, or words to that effect. I'm assuming this is conventional wisdom and that it applies to all dynamic languages. Why? What is it about dynamic languages that makes IOC containers unnecessary?

推荐答案

IoC提供了一种机制,可以打破当对象在另一个类上调用"new"时获得的耦合.这种耦合将调用对象与它实现的任何接口的实例化实现联系在一起.

IoC provides a mechanism to break the coupling you get when an object calls 'new' on another class. This coupling ties the calling object with the instantiated implementation of whatever interface it implements.

在静态语言中,当您按名称引用类(在其上调用new)时,没有歧义.这是对特定类别的紧密耦合.

In static languages when you reference a class by name (to call new on it), there is no ambiguity. This is a tight coupling to a specific class.

在动态语言中,调用new X是在执行时实例化定义为X的任何类"的占位符.这是松散耦合,因为它仅与名称X耦合.

In dynamic languages calling new X is a placeholder for "instantiate whatever class is defined as X at the point of execution". This is a looser coupling, as it's only coupled to the name X.

这种细微的差别意味着,在动态语言中,您通常可以更改X的含义,因此,在调用类之外仍可以修改关于实例化哪个类的决定.

This subtle difference means that in a dynamic language you can usually change what X is, so the decision as to which class is instantiated is still modifiable outside of the calling class.

但是,我个人发现IoC有两个优点,我不能依靠动态语言来允许注入.

However, personally I find there are two advantages to IoC that I don't get by relying on the dynamic language to allow injection.

通过构造函数传递依赖关系的副作用之一是,您最终会产生去耦",可重用且易于测试的构建块"类.他们不知道打算在什么上下文中使用它们,因此您可以在各处重复使用它们.

One of the side effects of passing dependencies in through constructors is that you end up with "building block" classes that are very decoupled, reusable and easy to test. They have no idea what context they are intended to be used in, so you can reuse them all over the place.

另一个结果是使用显式代码进行接线.正确完成后,就可以清楚地代表您的应用程序的结构,并将其分解为子系统和生命周期.这使人们可以明确地决定与类关联的生命周期或子系统(在编写接线代码时),并在编写类时专注于对象的行为.

The other result is having explicit code to do the wiring. Done correctly this cleanly represents the structure of your application and it's decomposition into subsystems and life-cycles. This makes people explicitly decide which life-cycle or subsystem they want to associate their class with (when writing the wiring code), and concentrate on the behavior of the object when writing the class.

就像JörgW Mittag所说.那些工具是不必要的,设计原理不是." 我相信它们是不必要的,但是做得正确,仍然很有价值.

Like Jörg W Mittag said.. "Those tools are unnecessary, the design principles aren't." I believe they are unnecessary, but done right, still valuable.

这篇关于为什么动态语言不需要IOC容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆