设计模式 – 这个模式的名称是否有闭包?

设计模式 – 这个模式的名称是否有闭包?,第1张

概述我经常看到在我们有访问对象之前需要执行的查找代码的情况下使用的模式.使用此模式时,通常以单词with开头. 例如,在我们可以使用之前,我们需要从数据库中检索客户记录: def withCustomer (id, closure) { def customer = getCustomer(id) closure(customer)}withCustomer(12345) { c 我经常看到在我们有访问对象之前需要执行的查找代码的情况下使用的模式.使用此模式时,通常以单词with开头.

例如,在我们可以使用之前,我们需要从数据库中检索客户记录:

def withCustomer (ID,closure) {    def customer = getCustomer(ID)    closure(customer)}withCustomer(12345) { customer ->    println "Found customer $customer.name"}

Groovy在闭包或匿名函数之间没有这样的区别.也许,我可以问一下这个模式是否有匿名函数的名称.

@H_301_18@解决方法 这是战略模式.闭包持有一些行为作为参数传递给函数,因此函数可以接受不同的行为.参见Peter norvig的演讲 Design Patterns in Dynamic Languages:

The strategy is a variable whose value is a function (E.g.,with first-class functions,pattern is invisible)

总结

以上是内存溢出为你收集整理的设计模式 – 这个模式的名称是否有闭包?全部内容,希望文章能够帮你解决设计模式 – 这个模式的名称是否有闭包?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1224870.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存