使用生成器的 C# 构造对象

发布于 2024-12-28 19:40:16 字数 973 浏览 2 评论 0原文

Fluent 构建器是一种众所周知的构建具有许多属性的对象的模式:

Team team = teamBuilder.CreateTeam("Chelsea")
    .WithNickName("The blues")
    .WithShirtColor(Color.Blue)
    .FromTown("London")
    .PlayingAt("Stamford Bridge");

但是,由于一个特定的原因,使用它对我来说似乎不太清楚:

  • 每个 Team 对象都有其最小操作状态,换句话说,必须设置的属性集(强制),以便对象可以使用。

现在,考虑到必须维持这种状态,应该如何使用 Fluent builder 方法?

With_XYZ 成员是否应该修改对象的一部分,而不会影响此状态?

也许这种情况有一些通用规则?


更新:

如果CreateTeam方法应将强制属性作为参数,那么接下来会发生什么?

  • 如果我(例如)省略 WithNickName 调用,会发生什么?

  • 这是否意味着昵称应该默认为某个DefaultNickname

  • 这是否意味着该示例(请参阅链接)很糟糕,因为该对象可能处于无效状态?

  • 而且,我怀疑在这种情况下,流畅的构建方法实际上失去了它的“美感”,不是吗?

Fluent builder is a well-known pattern to build objects with many properties:

Team team = teamBuilder.CreateTeam("Chelsea")
    .WithNickName("The blues")
    .WithShirtColor(Color.Blue)
    .FromTown("London")
    .PlayingAt("Stamford Bridge");

However, using it doesn't seem very clear to me due to one particular reason:

  • Every Team object has its minimal operational state, in other words, set of properties which have to be set (mandatory), so that the object is ready to use.

Now, how should the Fluent builder approach be used considering that you have to maintain this state?

Should the With_XYZ members modify the part of the object, that can't affect this state?

Maybe there are some general rules for this situation?


Update:

If the CreateTeam method should take the mandatory properties as arguments, what happens next?

  • What happens if I (for example) omit the WithNickName call?

  • Does this mean that the nickname should be defaulted to some DefaultNickname?

  • Does this mean that the example (see the link) is bad, because the object can be left in invalid state?

  • And, well, I suspect that in this case the fluent building approach actually loses it's "beauty", doesn't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白首有我共你 2025-01-04 19:40:16

CreateTeam() 应该将强制属性作为参数。

Team CreateTeam(string name, Color shirtColor, string Town)
{
}

CreateTeam() should have the mandatory the properties as parameters.

Team CreateTeam(string name, Color shirtColor, string Town)
{
}
禾厶谷欠 2025-01-04 19:40:16

在我看来,Fluent Interface 的要点是:

  • 将构造函数中的参数数量最小化为零,同时在创建时仍然动态初始化某些属性。
  • 使属性/参数值关联非常清晰 - 在大型参数列表中,什么值代表什么?如果不进一步挖掘就无法判断。
  • 实例化的编码风格非常干净、可读且可编辑。使用此格式样式添加或删除属性设置不太容易出错。 IE 删除整行,而不是在长参数列表的中间进行编辑;更不用说编辑错误的参数了

Seems to me the points of Fluent Interface are:

  • Minimize the number of parameters to zero in a constructor while still dynamically initializing certain properties upon creation.
  • Makes the property/ parameter-value association very clear - in a large parameter list, what value is for what? Can't tell without digging further.
  • The coding style of the instantiation is very clean, readable, and editable. Adding or deleting property settings with this formatting style is less error prone. I.E. delete an entire line, rather than edit in the middle of a long parameter list; not to mention editing the wrong parameter
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文