这个类的名称应该是什么?

发布于 2024-08-27 14:39:11 字数 1976 浏览 4 评论 0原文

命名类有时很困难。你认为班级名称应该是什么?

我最初创建该类是为了用作缓存,但可以看到它可能还有其他用途。使用该类的示例代码。

Dim cache = New NamePendingDictionary(Of String, Sample)

Dim value = cache("a", Function() New Sample())

这是需要名称的类。

' <summary> '
' Enhancement of <see cref="System.Collections.Generic.Dictionary"/>. See the Item property '
' for more details. '
' </summary> '
' <typeparam name="TKey">The type of the keys in the dictionary.</typeparam> '
' <typeparam name="TValue">The type of the values in the dictionary.</typeparam> '
Public Class NamePendingDictionary(Of TKey, TValue)
    Inherits Dictionary(Of TKey, TValue)

    Delegate Function DefaultValue() As TValue

    ' <summary> '
    ' Gets or sets the value associated with the specified key. If the specified key does not exist '
    ' then <paramref name="createDefaultValue"/> is invoked and added to the dictionary. The created '
    ' value is then returned. '
    ' </summary> '
    ' <param name="key">The key of the value to get.</param> '
    ' <param name="createDefaultValue"> '
    ' The delegate to invoke if <paramref name="key"/> does not exist in the dictionary. '
    ' </param> '
    ' <exception cref="T:System.ArgumentNullException"><paramref name="key" /> is null.</exception> '
    Default Public Overloads ReadOnly Property Item(ByVal key As TKey, ByVal createDefaultValue As DefaultValue) As TValue
        Get

            Dim value As TValue

            If createDefaultValue Is Nothing Then
                Throw New ArgumentNullException("createValue")
            End If

            If Not Me.TryGetValue(key, value) Then

                value = createDefaultValue.Invoke()
                Me.Add(key, value)

            End If

            Return value

        End Get

    End Property

End Class

编辑:根据 Abel 的建议,我将该类命名为 ValueCache。

Naming classes is sometimes hard. What do you think name of the class should be?

I originally created the class to use as a cache but can see its may have other uses. Example code to use the class.

Dim cache = New NamePendingDictionary(Of String, Sample)

Dim value = cache("a", Function() New Sample())

And here is the class that needs a name.

' <summary> '
' Enhancement of <see cref="System.Collections.Generic.Dictionary"/>. See the Item property '
' for more details. '
' </summary> '
' <typeparam name="TKey">The type of the keys in the dictionary.</typeparam> '
' <typeparam name="TValue">The type of the values in the dictionary.</typeparam> '
Public Class NamePendingDictionary(Of TKey, TValue)
    Inherits Dictionary(Of TKey, TValue)

    Delegate Function DefaultValue() As TValue

    ' <summary> '
    ' Gets or sets the value associated with the specified key. If the specified key does not exist '
    ' then <paramref name="createDefaultValue"/> is invoked and added to the dictionary. The created '
    ' value is then returned. '
    ' </summary> '
    ' <param name="key">The key of the value to get.</param> '
    ' <param name="createDefaultValue"> '
    ' The delegate to invoke if <paramref name="key"/> does not exist in the dictionary. '
    ' </param> '
    ' <exception cref="T:System.ArgumentNullException"><paramref name="key" /> is null.</exception> '
    Default Public Overloads ReadOnly Property Item(ByVal key As TKey, ByVal createDefaultValue As DefaultValue) As TValue
        Get

            Dim value As TValue

            If createDefaultValue Is Nothing Then
                Throw New ArgumentNullException("createValue")
            End If

            If Not Me.TryGetValue(key, value) Then

                value = createDefaultValue.Invoke()
                Me.Add(key, value)

            End If

            Return value

        End Get

    End Property

End Class

EDIT: On Abel's advice I've named the class ValueCache.

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

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

发布评论

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

评论(4

南街九尾狐 2024-09-03 14:39:11

一般来说,最好根据类的预期用途来命名它。如果用户后来发现其他用途是可能的或可行的,请不要立即重命名您的类。重命名类的原因应该只是为了使其预期用途更清晰。

(编辑)其他人对新名称发表了评论,例如 CacheManagerDeferredCacheLazyCollectionAssignedValueMap 等。初衷很笼统,就用这样的名字。如果预期用途更具体,请将其命名为:CookiesCacheUsersList 等。

如果您发现自己处于其他特定类的通用用例的情况,创建一个更通用的基类,具有通用名称,并针对特定(原始)用例使用特定子类。这就是面向对象的意义;-)

In general, it's best to name a class after its intended usages. If users later find that another usage is possible or feasible, don't run off renaming your class. A reason to rename your class should only be to make its intended use clearer.

(edit) Others have commented on new names, like CacheManager, DeferredCache, LazyCollection, AssignedValueMap etc. If the original intention is very generic, use such names. If the intended use is more specific, name it such: CookiesCache, UsersList etc.

If you find yourself in the situation where you have a generic use case for an otherwise specific class, create a more general base class, with a general name, and use a specific subclass for the specific (original) use case. That's what OO is about ;-)

梦幻之岛 2024-09-03 14:39:11

您可以将其称为 LazyDictionary,因为项目在需要时可能不会被初始化:)

什么是 createDefaultValue?这是作为构造函数的一部分初始化的吗?

You could call it a LazyDictionary, since items may not be initialized until they are needed :)

What is createDefaultValue? Is this initialized as part of the constructor?

装纯掩盖桑 2024-09-03 14:39:11

调用此类CacheManager,并且不要在其中放置其他功能。

Call this class CacheManager and DO NOT put other functionaities in it.

盗琴音 2024-09-03 14:39:11

我认为正如阿贝尔所建议的,“地图”是一个很好用的术语。我会考虑将类命名为“AssignedValueMap”之类的名称,然后使用更具体的变量名称来明确在每种情况下如何使用“AssignedValueMap”。因此,当您将它用于待定名称时,我会将关联变量声明为 DimendingNamesMap = NewAssignedValueMap(OfString, Sample)。

I think as Abel suggested Map is a good term to use. I would consider naming the class something like AssignedValueMap and then use more specific variable names to make clear how you're using the AssignedValueMap in each case. So where you're using it for pending names I'd declare the associated variable as Dim pendingNamesMap = New AssignedValueMap(Of String, Sample).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文