所以,你会创建这样一个流畅的接口:
class FluentTable
{
//as a dumb example I'm using a list
//use whatever structure you need to store your items
List<string> myTables = new List<string>();
public FluentTable addTableCellwithCheckbox(string chk, bool isUnchecked,
bool isWithoutLabel)
{
this.myTables.Add(chk);
//store other properties somewhere
return this;
}
public FluentTable addTableCellwithTextbox(string name, bool isEditable)
{
this.myTables.Add(name);
//store other properties somewhere
return this;
}
public static FluentTable New()
{
return new FluentTable();
}
}
现在你可以使用这样的:
FluentTable tbl = FluentTable
.New()
.addTableCellwithCheckbox("chkIsStudent", true, false)
.addTableCellwithTextbox("tbStudentName", false);
这应该给你的,你需要如何去了解一个基本的想法它。请参阅fluent interfaces on wikipedia。
做一个比较正确的做法是将实现一口流利的接口:
interface IFluentTable
{
IFluentTable addTableCellwithCheckbox(string chk, bool isUnchecked,
bool isWithoutLabel)
IFluentTable addTableCellwithTextbox(string name, bool isEditable)
//maybe you could add the static factory method New() here also
}
,然后实现它:class FluentTable : IFluentTable {}
所有的答案都很好,但这个真的超越,谢谢这真的很有帮助 – 2012-04-01 20:24:01