Fluent Interface(流式接口)
2016-07-27 22:57
483 查看
我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有Martin fowler大师 所写http://martinfowler.com/bliki/FluentInterface.html
Fluent Interface实例
Java 类Country
[java] view plain copy
package com.jue.fluentinterface;
public class Country {
private String name;
private int code;
private boolean isDevelopedCountry;
private int area;
Country addName(String name) {
this.name = name;
return this;
}
Country addCountyCode(int code) {
this.code = code;
return this;
}
Country setDeveloped(boolean isdeveloped) {
this.isDevelopedCountry = isdeveloped;
return this;
}
Country setAread(int area) {
this.area = area;
return this;
}
}
调用类
[java] view plain copy
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Country china = new Country();
china.addName("The People's Republic of China")
.addCountyCode(1001)
.setDeveloped(false)
.setAread(960);
}
主要特征:
Country 的方法返回本身country,使调用者有了继续调用country方法的能力.
优势
1.有时候我们需要根据传入的参数数目不同定义不同的构造器。使用 FluentInterface就可以随意传递想要的数据,并保持他们的连贯。
java中的应用
Fluent Interface实例
Java 类Country
[java] view plain copy
package com.jue.fluentinterface;
public class Country {
private String name;
private int code;
private boolean isDevelopedCountry;
private int area;
Country addName(String name) {
this.name = name;
return this;
}
Country addCountyCode(int code) {
this.code = code;
return this;
}
Country setDeveloped(boolean isdeveloped) {
this.isDevelopedCountry = isdeveloped;
return this;
}
Country setAread(int area) {
this.area = area;
return this;
}
}
调用类
[java] view plain copy
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Country china = new Country();
china.addName("The People's Republic of China")
.addCountyCode(1001)
.setDeveloped(false)
.setAread(960);
}
主要特征:
Country 的方法返回本身country,使调用者有了继续调用country方法的能力.
优势
1.有时候我们需要根据传入的参数数目不同定义不同的构造器。使用 FluentInterface就可以随意传递想要的数据,并保持他们的连贯。
java中的应用
StringBuffer append方法
相关文章推荐
- 使用 setuid、setgid 和 Sticky 位的特殊文件权限
- [置顶]Request 和 Response 原理
- 关于监听(安卓)
- 关于新导入java项目后提示:Configure build path
- android UI基本控件
- HDU4027:Can you answer these queries?(线段树 单点更新 区间求和)
- unique函数的使用
- poj 2299 Ultra-QuickSort
- 【POJ2299】Ultra-Quicksort-逆序对
- Codeforces670E. Correct Bracket Sequence Editor
- DualPivotQuicksort两枢轴快速排序
- Android Bluetooth(蓝牙)实例
- mysql生成不重复随机数(unique number generation)
- Vue 经验总结
- UITableView全面解析文章
- poj 2299 Ultra-QuickSort (归并求逆序数)
- Bluetooth-->蓝牙开发扫描设备,及蓝牙设备类型
- EasyUI 网页边框布局
- EasyUI 菜单与按钮
- EasyUI 拖放