Fluent Interface-感知

来源:互联网 发布:js 骰子的转动效果 编辑:程序博客网 时间:2024/05/01 12:56

我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有Martin fowler大师 所写http://martinfowler.com/bliki/FluentInterface.html

Fluent Interface实例

Java 类Country

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;}}

调用类

/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubCountry 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方法

原创粉丝点击