枚举多模式
如果 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您链接到的模式的描述没有一组固定的键,所以我不确定如果您有一组固定的键,您可以将其称为 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.
在 C# 中,我经常使用一种我称之为“智能枚举”或“重枚举”的模式,其中我想要一些你可以认为是 (a) 其值为对象的枚举(如 Java 允许的),或 (b)类中所有可能的实例都是在编译时定义的(这就是我实现它的方式)。例如,我对音乐模式的概念进行了建模,如下所示:
当我第一次了解 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:
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?
我认为依赖注入工具如 Guice 或 Spring 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.