gpt4 book ai didi

c# - 什么时候应该封装泛型类型?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:11:02 38 4
gpt4 key购买 nike

我看到很多人建议您应该使用更接近您领域的类来封装泛型类型,例如 Steve 和 Nat 在 Growing Object-Oriented Software, Guided by Tests 中建议那:

Our rule of thumb is that we try to limit passing around types with generics [...]. Particularly when applied to collections, we view it as a form of duplication. It’s a hint that there’s a domain concept that should be extracted into a type.

一般来说,什么时候做这样的事情是个好主意..

class PersonList : List<Person>

.. 而不是仅仅使用 List<Person>直接地?

最佳答案

您正在寻找的是用于 Java 或 C# 的 typedef 运算符。

不幸的是,子类化方法不能很好地替代 typedef

以下文章"Java theory and practice: The pseudo-typedef antipattern"详细解释原因。

我将在此处逐字复制该文章的结论:

The motivation for the pseudo-typedef antipattern is straightforward enough -- developers want a way to define more compact type identifiers, especially as generics make type identifiers more verbose. The problem is that this idiom creates tight coupling between code that employs it and that code's clients, inhibiting reuse. You may not like the verbosity of generic type identifiers, but this is not the way to solve it.

关于c# - 什么时候应该封装泛型类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7241592/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com