什么是clojure等同于google guice? [英] What is the clojure equivalent to google guice?

查看:157
本文介绍了什么是clojure等同于google guice?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到google guice,不能真正理解它,它做了什么,虽然似乎有很多炒作。我希望能得到一个clojure的视角的图书馆,为什么在clojure应用程序需要/不需要,如果有任何类似的内置的语言。

解决方案

由于Java的OO和类型系统,在不同的底层实现(例如用于测试(模拟))之间动态切换可能难以管理。像Google Guice这样的图书馆旨在更加优雅地处理这些 Java依赖注入



在Clojure和其他函数式语言中,函数可以传递,这使得使用不同的实现更容易。



在Clojure中有几种方法可以实现:




  • 使用您在高阶函数中选择的函数作为参数。






  • Clojure编程的第12章有一些



    Sean Devlin还有一个完整Disclojure视频在Clojure中的依赖注入。他的例子可能被选择得更好。他不使用完全不同的函数实现在他的闭包,他使用一个工厂返回不同的版本的函数。



    基本上,依赖注入是一种模式,在OOP中是一个必要的邪恶,可以很容易地解决(甚至不是一个问题)在FP。


    I came across google guice and could not really understand it and what it did, although there seems to be alot of hype around it. I was hoping to get a clojurian perspective of the library and why it is needed/not needed in clojure applications and if there was anything similar built into the language.

    解决方案

    Because of Java's OO and type system, dynamically switching between different underlying implementations (for test (mocking) purposes for instance) can be difficult to manage. Libraries like Google Guice are intended to handle these dependency injections in Java more gracefully.

    In Clojure and other functional languages functions can be passed around, which makes using different implementations much easier.

    There's several ways this can be done in Clojure:

    • Using your choice of function as parameters in higher order functions.
    • (Re)Binding your choice of function to a var.
    • Encapsulating your choice of function inside closures that can then be passed around and called.

    Chapter 12 of Clojure Programming has some nice examples of OO patterns like dependency injection and the alternative ways to handle these in Clojure.

    Sean Devlin also has a Full Disclojure video on Dependency Injection in Clojure. His example might have been chosen better, though. Instead of using completely different function implementations in his closure, he uses a factory that returns different 'versions' of a function. The gist stays the same though.

    Basically, dependency injection is a pattern that is a necessary evil in OOP, and can be solved easily (or is not even a problem) in FP.

    这篇关于什么是clojure等同于google guice?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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