Den*_*rax 3 c# methods design-patterns class function
考虑我在我的简单服务器框架中有多个函数,如下所示,它需要多个函数,如下所示:
new TestServer()
.DOBind("ip", "port")
.SetMaxBindTries(3)
.SetMaxConnections(300)
.ONBind(delegate {...})
.ONReceiveBytes(delegate {...})
.ONSocketStatusChanged(delegate {...})
.ONBlaBlaBla...
Run Code Online (Sandbox Code Playgroud)
我的问题: - 我怎么能这样做? - 调查的"特殊关键词"是什么? - 我应该遵循什么样的"类设计/设计模式"结构?
任何指针赞赏.
这里没有秘密的关键字或设计.会发生什么是这些方法中的每一个都返回TestServer的实例(只需返回this):
TestServer DoThis()
{
// method code
return this;
}
TestServer DoThat(string WithThisParameter)
{
// method code
return this;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
var x = new TestServer();
x.DoThis().DoThat("my string").DoThis();
Run Code Online (Sandbox Code Playgroud)
显然,正如Vache dee-see在评论中所写,这被称为"流畅的API"