English 中文(简体)
我如何在Clojure中制造一个类似 Java的物体,使用建筑模式?
原标题:How do I create a Java-like object in Clojure that uses builder pattern?
  • 时间:2012-01-11 15:21:04
  •  标签:
  • clojure

我如何使用克隆技术制造以下物体? 物体取自 j(From Effective Java):

NutritionFacts cocaCola = new NutritionFacts.Builder(240,8).calories(100).sodium(35).carbohydrate(27).build();

最佳回答

虽然很难用其他答复中的分数表示:“......” 1 /sup” ,但“条形” 。 我个人希望:

(-> (NutritionFacts$Builder. 240 8) 
    (.calories 100)
    (.sodium 350)  
    (.carbohydrates 27) 
    (.build))

这有两个特点:

  • Explicitness. I can look at the sodium line (for example) and tell it s a Java method call, because the . is right there.
  • Flexibility. If I need to, I can chain some non-method call in the middle there (printing it to stdout, say), or at the end of all this feed it in to some other function call.

最重要的是,对这一问题的所有其他答复都夸大了等级错误: Java s NutritionFacts. 食糖是真正称为“营养食品”的JV级体内的语言糖,而这一类是一种服装必须指的(因为我们没有使用javac来汇编我们的代码)。

<>1> 我不同意<代码>doto的建议:该建议之所以可行,只是因为这一建筑商的班子会以一个单一场合的突变实施其方法链,然后返回。 doto对 Java物体而言是巨大的,这些物体需要内部的突变,但如果一个类别足以预示其可变性,则真正应当使用方法链式版本(例如,->)。

问题回答

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/.rel=“nofollow”>。 它分两条。 这只允许你需要做些什么,即根据以前的结果,连续使用下 Java法。

我没有再保留地,但你的行文应当转化为:

(.. (NutritionFacts.Builder. 240 8) 
    (calories 100)
    (sodium 350)  
    (carbohydrates 27) 
    (build))

我只从克隆开始,但像标准方法一样在我身上援引:

(doto
  (NutritionFacts.Builder. 240 8)
  (.carbohydrates 27)
  (.sodium 35)
  (.calories 100)
  (.build)
)

EDIT:
As @Goran Jovic points out, this calls all methods on the object created in the first form.
So it works in this case since the Java code uses method chaining, but is not more generally applicable.

www.un.org/chinese/ga/president (NutrionalFacts.Builder. 240 8) (calories 100)(35) (carbohydrates 27) (build)

This thread is a couple of years old, and the thread-first macro is still preferred way as far as i can tell, but the $ syntax is not needed. The slash (/) works as well:

(-> (Caffeine/newBuilder)
      (.maximumSize 10000)
      (.build))




相关问题
How to improve Clojures error messages

I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...

clojure rmi classpath problem

I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...

Help translating this Java codeblock to Clojure?

I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

how to efficiently apply a medium-weight function in parallel

I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...

热门标签