什么时候以及如何使用枚举类而不是枚举? [英] When and how should I use enumeration classes rather than enums?

查看:86
本文介绍了什么时候以及如何使用枚举类而不是枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在工作的开发人员开始使用类模式,而不是在通常适合枚举的地方使用枚举.取而代之的是,他使用了类似于以下内容的东西:

A developer at work recently started using a class pattern instead of enums in places where enums would usually fit. Instead, he uses something similar to that below:

internal class Suit
{
    public static readonly Suit Hearts = new Suit();
    public static readonly Suit Diamonds = new Suit();
    public static readonly Suit Spades = new Suit();
    public static readonly Suit Clubs = new Suit();
    public static readonly Suit Joker = new Suit();
    private static Suit()
    {

    }
    public static bool IsMatch(Suit lhs, Suit rhs)
    {
        return lhs.Equals(rhs) || (lhs.Equals(Joker) || rhs.Equals(Joker));
    }
}

他的推理是,它看起来像是枚举,但是却允许他包含与枚举相关的方法(如上面的IsMatch一样).

His reasoning is that it invisibly looks like an enumeration, but allows him to contain the methods relating to the numeration (like the IsMatch above) to be contained within the enumeration itself.

他将其称为枚举类,但这不是我以前见过的.我想知道优点和缺点是什么,在哪里可以找到更多信息?

He called this an Enumeration class, but it's not something I've ever seen before. I wondered what the advantages and disadvantages were and where I could find out more information?

谢谢

他描述的另一个优点是能够为枚举添加特定的ToString()实现.

Another advantage he described was being able to add a specific ToString() implementation for the enumeration.

推荐答案

枚举的主要优点是它们基本上是带有某些命名值的整数,因此它们固有地可移植且可序列化.枚举的算术和逻辑运算也更快.

The main advantage of enums is that they are basically integers with some named values, as such they are inherently portable and serializable. Arithmetic and logical operations are also faster on enums.

枚举类在需要具有附加状态信息的不透明值时使用.例如,通用数据访问层可以具有以下接口:

Enumeration classes are used when you need an opaque value which has extra state information. For instance a generic Data Access Layer could have as an interface like:


public static class Dal
{
    public static Record Query(Operation operation, Parameters parameters);
}

var result = Dal.Query(Operation.DoSomething, new DoSomethingParameters {...});

对Dal操作的用户来说,它只是可用操作的枚举,但它可以包含连接字符串,SQL语句或存储过程以及通用Dal所需的任何其他数据.

To the users of the Dal Operation is just an enumeration of the operations available, but it could contain the connection string, the SQL statement or stored procedure and any other data needed by the generic Dal.

另一种公共"用法是用于系统(状态或策略)中的公共模式.从用户的角度来看,模式是一个不透明的值,但它可能包含对系统的实现至关重要的信息或内部功能.一个人为的例子:

Another "common" use is for public modes in a system (a state or a strategy). From a user perspective the mode is an opaque value, but it may contain information or internal functionality crucial to the implementation of the system. A contrived example:



public class TheSystem
{
   public SystemMode Mode;
   public void Save()
   {
      Mode.Save();
   }
   public SystemDocument Read()
   {
      return Mode.Read();
   }
}

public abstract class SystemMode
{
   public static SystemMode ReadOnly = new ReadOnlyMode();
   public static SystemMode ReadWrite = new ReadWriteMode();

   internal abstract void Save();
   internal abstract SystemDocument Read();

   private class ReadOnlyMode : SystemMode
   {
      internal override void Save() {...}
      internal override SystemDocument Read() {...}
   }

   private class ReadWriteMode : SystemMode
   {
      internal override void Save() {...}
      internal override SystemDocument Read() {...}
   }
}


TheSystem.Mode = SystemMode.ReadOnly;

我不认为仅使用IsMatch静态方法就可以保证不使用简单的枚举.在这种情况下,使用扩展方法可以实现非常相似的事情.

I don't think just having an IsMatch static method warrants not using simple enums. Something very similar could be achieved with extension methods in this case.

这篇关于什么时候以及如何使用枚举类而不是枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆