您的位置:首页 > 产品设计 > UI/UE

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中的应用

StringBuffer append方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: