枚举多模式

发布于 2024-10-19 03:01:11 字数 282 浏览 2 评论 0原文

如果 Multiton 的实例数量在编译时是固定的,那么对 Multiton 模式 使用枚举是个好主意-time.我已经看到了 Enum Sigleton 模式,所以我只是想知道是否Multiton 也可以做类似的事情吗?

Is a good idea to use enum's for Multiton pattern if the number of instances for Multiton is fixed at compile-time.I have seen the Enum Sigleton pattern,so i was just wondering if the similar can be done for Multiton's too ?

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

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

发布评论

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

评论(3

金兰素衣 2024-10-26 03:01:11

您链接到的模式的描述没有一组固定的键,所以我不确定如果您有一组固定的键,您可以将其称为 Multiton。但如果我们接受它,那么是的,每个枚举实例都是单例。

请记住,枚举是对象:它们可以具有状态和方法。

The description of the pattern you linked to doesn't have a fixed set of keys, so I'm not sure you could call it a Multiton if you have a fixed set of keys. But if we accept it, then yes, enum instances each are a singleton.

Remember that enums are objects : they can have state and methods.

橘虞初梦 2024-10-26 03:01:11

在 C# 中,我经常使用一种我称之为“智能枚举”或“重枚举”的模式,其中我想要一些你可以认为是 (a) 其值为对象的枚举(如 Java 允许的),或 (b)类中所有可能的实例都是在编译时定义的(这就是我实现它的方式)。例如,我对音乐模式的概念进行了建模,如下所示:

public class MusicalMode
{
    public readonly String Name;
    public readonly int Number;   // starting reference note: 1=C, 2=D, etc.
    public readonly int[] Intervals;  // 1 = half step, 2 = whole step

    public string ToString () 
    { /* implementation omitted */ }  

    public static MusicalMode FromString (String s) 
    { /* implementation omitted */ }

    // these are the only instances that can ever exist
    public static readonly Mode Ionian     = new Mode(1, "Ionian");
    public static readonly Mode Dorian     = new Mode(2, "Dorian");
    public static readonly Mode Phrygian   = new Mode(3, "Phrygian");
    public static readonly Mode Lydian     = new Mode(4, "Lydian");
    public static readonly Mode Mixolydian = new Mode(5, "Mixolydian");
    public static readonly Mode Aeolian    = new Mode(6, "Aeolian");
    public static readonly Mode Locrian    = new Mode(7, "Locrian");

    private Mode (int num, String name)
    { /* implementation omitted */ }
}

当我第一次了解 Multiton 模式时,我想“哦,我已经做到了!”,想到了我的智能/重型枚举。但现在我不确定,根据 JB Nizet 关于密钥集是否固定的评论(如我的示例中所示)。

我的 MusicalMode 是 Multiton 的示例吗?如果没有,该模式是否有一个公认的名称?

In C# I've often used a pattern I call "smart enums" or "heavy enums", where I want something you can think of as (a) an enum whose values are objects (like Java allows), or (b) a class in which all the possible instances are defined at compile-time (which is how I implement it). For example, I've modeled the concept of musical mode like this:

public class MusicalMode
{
    public readonly String Name;
    public readonly int Number;   // starting reference note: 1=C, 2=D, etc.
    public readonly int[] Intervals;  // 1 = half step, 2 = whole step

    public string ToString () 
    { /* implementation omitted */ }  

    public static MusicalMode FromString (String s) 
    { /* implementation omitted */ }

    // these are the only instances that can ever exist
    public static readonly Mode Ionian     = new Mode(1, "Ionian");
    public static readonly Mode Dorian     = new Mode(2, "Dorian");
    public static readonly Mode Phrygian   = new Mode(3, "Phrygian");
    public static readonly Mode Lydian     = new Mode(4, "Lydian");
    public static readonly Mode Mixolydian = new Mode(5, "Mixolydian");
    public static readonly Mode Aeolian    = new Mode(6, "Aeolian");
    public static readonly Mode Locrian    = new Mode(7, "Locrian");

    private Mode (int num, String name)
    { /* implementation omitted */ }
}

When I first learned about the Multiton pattern, I thought "Oh, I've done that!", thinking of my smart/heavy enums. But now I'm not sure, based on JB Nizet's remark about whether the set of keys is fixed (as it is in my example).

Is my MusicalMode an example of a Multiton? If not, is there a well-established name for that pattern?

北风几吹夏 2024-10-26 03:01:11

我认为依赖注入工具如 GuiceSpring Framework IoC 现在更适合此目的,因为您可以管理范围、状态和其他有趣的东西。

I think Dependency Injection tools like Guice or Spring Framework IoC is more preferable for this purpose now as you can manage scope, state and other interesting stuff.

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