流式接口
流式接口(fluent interface)是软件工程中面向对象API的一种实现方式,以提供更为可读的源代码。
概述
C++的iostream流式调用就是一个典型的例子。Smalltalk在1970年代就实现了方法瀑布调用。
JavaScript
JavaScript使用原型继承与`this`.
Java
jOOQ库模拟了SQL
C#
C#在LINQ中大量使用 standard query operators与扩展方法。
流式接口可用于一系列方法,他们运行在同一对象上。
Ruby
Ruby语言允许修改核心类,这使得流式接口成为原生易于实现。
Perl 6
In Perl 6, there are many approaches, but one of the simplest is to declare attributes as read/write and use the keyword. The type annotations are optional, but the native gradual typing makes it much safer to write directly to public attributes.
PHP
在PHP中,可以使用表示实例的特殊变量$this返回当前对象。因此返回$this将使方法返回实例。下面的示例定义了一个Employee类和三个方法来设置它的名称、姓和薪水。每个Employee类的实例允许调用这些方法。
参考来源
- 完整原文与贡献者 · CC BY-SA 4.0 · 原文整理