博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fluent interface
阅读量:6500 次
发布时间:2019-06-24

本文共 3659 字,大约阅读时间需要 12 分钟。

In 
, a 
fluent interface
 (as first coined by 
 and 
) is an implementation of an 
 API that aims to provide for more readable code.

A fluent interface is normally implemented by using  (concretely ) to relay the instruction context of a subsequent call (but a fluent interface entails more than just method chaining ). Generally, the context is

  • defined through the return value of a called method
  • self-referential, where the new context is equivalent to the last context
  • terminated through the return of a void context.

 

History[]

The term "fluent interface" was coined in late 2005, though this overall style of interface dates to the invention of method cascading in Smalltalk in the 1970s, and numerous examples in the 1980s. The most familiar is the  library in C++, which uses the << or >> for the message passing, sending multiple data to the same object and allowing "manipulators" for other method calls. Other early examples include the  (from 1988 in Lisp) and the  (from 1994 in C++) which used this style for object creation and property assignment.

 

Examples[]

Java[]

The  library models SQL as a fluent API in Java

1
2
3
4
5
6
Author a = AUTHOR.as(
"a"
);
create.selectFrom(a)
      
.where(exists(selectOne()
                   
.from(BOOK)
                   
.where(BOOK.STATUS.eq(BOOK_STATUS.SOLD_OUT))
                   
.and(BOOK.AUTHOR_ID.eq(a.ID))));

  

The  library enables the use of fluent code for performing auxiliary tasks like structure iteration, data conversion, filtering, etc.

1
2
3
4
String[] datesStr = 
new 
String[] {
"12-10-1492"
"06-12-1978"
};
...
List<Calendar> dates =
    
Op.on(datesStr).toList().map(FnString.toCalendar(
"dd-MM-yyyy"
)).get();

  

The  annotation processor enables the creation of a fluent API using Java annotations.

Also, the  testing library  makes extensive use of this style of interface to provide an expressive programming interface.

1
2
Collection mockCollection = EasyMock.createMock(Collection.
class
);
EasyMock.expect(mockCollection.remove(
null
)).andThrow(
new 
NullPointerException()).atLeastOnce();

  

In the Java Swing API, the LayoutManager interface defines how Container objects can have controlled Component placement. One of the more powerful LayoutManager implementations is the GridBagLayout class which requires the use of the GridBagConstraints class to specify how layout control occurs. A typical example of the use of this class is something like the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
GridBagLayout gl = 
new 
GridBagLayout();
JPanel p = 
new 
JPanel();
p.setLayout( gl );
  
JLabel l = 
new 
JLabel(
"Name:"
);
JTextField nm = 
new 
JTextField(
10
);
  
GridBagConstraints gc = 
new 
GridBagConstraints();
gc.gridx = 
0
;
gc.gridy = 
0
;
gc.fill = GridBagConstraints.NONE;
p.add( l, gc );
  
gc.gridx = 
1
;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 
1
;
p.add( nm, gc );

  

This creates a lot of code and makes it difficult to see what exactly is happening here. The Packer class, visible at , provides a Fluent mechanism for using this class so that you would instead write:

1
2
3
4
5
6
7
8
JPanel p = 
new 
JPanel();
Packer pk = 
new 
Packer( p );
  
JLabel l = 
new 
JLabel(
"Name:"
);
JTextField nm = 
new 
JTextField(
10
);
  
pk.pack( l ).gridx(
0
).gridy(
0
);
pk.pack( nm ).gridx(
1
).gridy(
0
).fillx();

  

There are many places where Fluent APIs can greatly simplify how software is written and help create an API language that helps users be much more productive and comfortable with the API because the return value of a method always provides a context for further actions in that context.

==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/p/3655461.html,如需转载请自行联系原作者
你可能感兴趣的文章
matlab建立桌面图标,在ubuntu16.04上创建matlab的快捷方式(实现方法)
查看>>
matlab 识别率,【caffe-Windows】识别率批量输出——matlab实现
查看>>
MATLAB里面的mghglobal函数,Matlab讨论区 - 声振论坛 - 振动,动力学,声学,信号处理,故障诊断 - Powered by Discuz!...
查看>>
smarty使用php代码,笑谈配置,使用Smarty技术_php
查看>>
oracle数据实际值限制,c# – Oracle数据库TNS密钥“数据源”的值长度超过了’128’的限制...
查看>>
silk v3 decoder php,解码转换QQ微信的SILK v3编码音频为MP3或其他格式
查看>>
延时关机命令 linux,windows定时关机和linux定时关机的方法(shutdown命令)
查看>>
linux内核最早延迟,工作队列是实现延迟的新机制,从2.5版本Linux内核开始提
查看>>
linux的操作命令文档,Linux文档操作命令
查看>>
linux不能访问80端口,lunux开放80端口(本地访问不了linux文件可能是这个原因)...
查看>>
linux查看与开启sshd服务,[转载]linux查看与开启sshd服务
查看>>
android单位转换小程序,微信小程序中rpx与rem单位转换
查看>>
html绝对定位重叠,HTML_firefox下绝对定位元素重叠造成不可点击问题,重构地图网站过程中碰到的,f - phpStudy...
查看>>
ps切图教程 android,PS前端切图完整教程
查看>>
html显示服务器状态,显示服务器时间并一直显示(html代码)
查看>>
在线html代码优化,网站seo优化html代码方法
查看>>
HTML如何把输入框变成必填值,required输入框为必填项
查看>>
在html中哪一个不是链接的目标属性,HTML试题
查看>>
android otg 挂载流程,android USB OTG功能如何打开及实现
查看>>
html属性board,pin_board.html
查看>>