1
在DSL page of groovy它顯示了的Groovy閉包的重用VS補充水分副本
def email(Closure cl) {
def email = new EmailSpec()
def code = cl.rehydrate(email, this, this)
code.resolveStrategy = Closure.DELEGATE_ONLY
code()
}
爲什麼他們呼籲rehydrate
而不只是分配委託關閉:
def email(Closure cl) {
def email = new EmailSpec()
cl.delegate = email
cl.resolveStrategy = Closure.DELEGATE_ONLY
cl()
}
換句話說,爲什麼我們需要一個封閉的副本,而不是重複使用給定的副本。我不一定看到使用補水的問題,但我也沒有看到需要,這告訴我有什麼我不理解
'rehydrate'不只是設置代理 –
@tim_yates我知道,它創建了一個克隆。我的問題是更多爲什麼文檔建議使用克隆而不是重複使用同一個對象並避免創建新對象。一定有一個原因 – Hilikus