`
happmaoo
  • 浏览: 4526445 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Closures for Java

阅读更多
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

一群“聪明人”写了一份草案,要求给Java增加closure功能。所谓closure,就是类似php里面经常用到的变量函数

public static void main(String[] args) {
int plus2(int x) { return x+2; }
int(int) plus2b = plus2;
System.out.println(plus2b(2));
}

那么它如何简化现有的代码呢?看下面三个例子

1 传统的代码

public interface Runnable {
void run();
}
public interface API {
void doRun(Runnable runnable);
}
public class Client {
void doit(API api) {
api.doRun(new Runnable(){
public void run() {
snippetOfCode();
}
});
}
}

2 简化的代码

public interface API {
void doRun(void() func);
}


And the client like this:
public class Client {
void doit(API api) {
api.doRun(() {snippetOfCode(); }); // 将函数直接写在这里,省略了一个对象
}
}

3 进一步简化的代码

Runnable runnable(final void() func) {
return new Runnable() {
public void run() { func(); }
};
}
Allowing the client to write this:
public class Client {
void doit(API api) {
api.doRun(runnable(() {snippetOfCode(); })); //直接把runnable作为一个函数盒子
}
}

可以说这个功能的提出,是为了将类的方法的定义变得更加的灵活,然而这种用到时才定义的设计理念和j2ee传统的组件型的开发思想相违背。Closure何去何从,我们拭目以待。



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1104952


分享到:
评论

相关推荐

    Java Closures and Lambda(Apress,2015)

    Java Closures and Lambda introduces you to significant new changes to the Java language coming out of what is termed Project Lambda. These new changes make their debut in Java 8, and their highlight ...

    Core Java for the Impatient

    , If you’re an experienced programmer, Horstmann’s practical insights and sample code will help you quickly take advantage of lambda expressions (closures), streams, and other Java language and ...

    Java SE 8 for the Really Impatient

    Now, with Java SE 8 for the Really Impatient , internationally renowned Java author Cay S. Horstmann concisely introduces Java 8’s most valuable new features (plus a few Java 7 innovations that haven...

    Closures-for-Java:一个用于创建伪闭包的简单库,利用 Java 的匿名类,但提供标准化的接口,该接口也是类型化的

    "Closures-for-Java" 是一个早期的开源库,它旨在为 Java 开发者提供一种机制,以便在不支持闭包的 Java 版本(主要是 Java 6 及以下)中模拟闭包的功能。闭包是一种强大的编程概念,允许函数作为值传递,能够捕获并...

    Functional Programming for Java Developers

    根据提供的文件内容,这是一本名为《Functional Programming for Java Developers》的书,作者为Dean Wampler,首次出版于2011年。这本书专门针对Java开发者,介绍了函数式编程(Functional Programming,简称FP)的...

    Java SE 8 for the Really Impatient(O'Reilly,2014)

    Now, with Java SE 8 for the Really Impatient, internationally renowned Java author Cay S. Horstmann concisely introduces Java 8’s most valuable new features (plus a few Java 7 innovations that haven...

    Pro.Java.8.Programming.3rd.Edition.1484206428

    How to use the new thread and I/O APIs for today's Java applications that must perform at enterprise and parallel scales How to use the improved collections APIs How to build a better Java UI/UX using...

    Pro.Java.8.Programming.3rd.Edition.1484206428.epub

    How to use the new thread and I/O APIs for today's Java applications that must perform at enterprise and parallel scales How to use the improved collections APIs How to build a better Java UI/UX using...

    Learn.Java.for.Android.Development_Apress.2010+src

    The book also briefly presents most (if not all) of Java version 7''s language features, although not much is said about closures or modules (which were not finalized at the time of writing). Java ...

    Making Java Groovy源码

    Making Java Groovy Kenneth A. Kousen 1.Easier Java 2.Closures, builders, and metaprogramming 3.Gradle for builds, Spock for testing 4.Groovy frameworks like Grails and Griffon 源码

    Java - The Well-Grounded Java Developer

    - **Dynamic Syntax**: Explanation of Groovy's dynamic syntax and how it differs from Java, including closures, traits, and metaclass programming. - **Integration with Java**: Discussion on seamless ...

    Pro Java 8 Programming(Apress,3ed,2015)

    You will also delve into more advanced topics like lambda expressions, closures, new i/o (NIO.2), enums, generics, XML, metadata and the Swing APIs for GUI design and development. By the end of the ...

    groovy programming(2006.12).pdf

    通过对比Java,Groovy提供了更为简洁的语法,例如闭包(closures)的使用,使得代码更加优雅和易于理解。 #### 2. 面向对象编程在Groovy中的应用 Groovy继承了Java的面向对象特性,但又在此基础上进行了扩展和改进。...

    groovy_in_action_draft_ch_01.pdf

    It offers several advanced features such as closures, dynamic typing, and the meta-object protocol (MOP), making it an attractive choice for developers who are already familiar with Java. This ...

    jna-4.2.2 官方原版下载

    Automatic mapping from Java to native functions, with simple mappings for all primitive data types Runs on most platforms which support Java Automatic conversion between C and Java strings, with ...

    Learning-Scala-Practical-Functional-Programming-for-the-JVM.pdf

    closures or blocks) to work with collections, but may be challenged with its static, generic-supporting type system. For these and any other developers who want to learn how to develop in the Scala ...

    expect4j.jar 1.0

    Especially when it used in Java which doesn't support closures natively. There are other libraries which offer a more concise API, e.g. enchanter (http://code.google.com/p/enchanter/). enchanter is a...

Global site tag (gtag.js) - Google Analytics