9

我正在运行 Pharo,而我只是在一个用例中,那种叫 Dependency Injection à la Guice的尖叫声。Smalltalk 有类似的东西吗?

我知道您可以通过显式传入您的依赖项来完成所有操作。但这对我来说感觉很尴尬和冗长。

4

3 回答 3

4

有一种 Smalltalk 方言,非常强调依赖注入。它扩展了语言,不仅方法名而且类名都使用动态查找。类名的新颖查找与方法的查找最相似,不同之处在于它通过一系列嵌套类而不是沿继承链向上冒泡。因此,您可以通过更改嵌套环境来更改注入的类。

要了解有关方言的更多信息,请点击此链接

于 2010-04-23T13:26:27.603 回答
1

With Guice, it looks like you define your classes to take certain interfaces as constructor parameters. Then you tell Guice "this interface maps to that class implementing said interface".

That sort've thing is completely unnecessary in Smalltalk, because Smalltalk classes only care about protocols.

If we translated the example into Smalltalk, we could pass any object we liked into the RealBillingService's constructor, as long as that object responded to #logChargeResult: and #logConnectException:, i.e., as long as that object implemented the protocol required of a TransactionLog.

Here's a link to a similar answer to the above.

于 2010-04-23T12:42:43.460 回答
0

我不是真正的专家,但我在谷歌上找到了这篇文章:http: //codebetter.com/blogs/jeremy.miller/archive/2006/05/05/144172.aspx

我希望这将引导您朝着正确的方向前进。
:)

于 2010-04-21T15:36:23.763 回答