Fluent Interface-感知

我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有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 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方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值