我应该严格避免在 Android 上使用枚举吗? [英] Should I strictly avoid using enums on Android?

查看:29
本文介绍了我应该严格避免在 Android 上使用枚举吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在如下界面中定义了一组相关的常量,例如 Bundle 键:

I used to define a set of related constants like Bundle keys together in an interface like below:

public interface From{
    String LOGIN_SCREEN = "LoginSCreen";
    String NOTIFICATION = "Notification";
    String WIDGET = "widget";
}

这为我提供了一种更好的方法来将相关常量组合在一起,并通过静态导入(而不是实现)来使用它们.我知道 Android 框架也以与 Toast.LENTH_LONGView.GONE 相同的方式使用常量.

This provides me a nicer way to group related constants together and used them by making a static import (not implements). I know Android framework also uses the constants in same way like Toast.LENTH_LONG, View.GONE.

然而,我经常觉得 Java 枚举 提供了更好、更强大的方式来表示常量.

However, I often feel that the Java Enums provide much better and powerful way to represent the constant.

但是在 Android 上使用 enums 是否存在性能问题?

But is there a performence issue in using enums on Android?

经过一些研究,我最终陷入了困惑.从这个问题"避免在只有你的地方使用枚举需要从 Android 的性能提示中删除 Ints? 很明显 Google 已经从其性能提示中删除了避免枚举",但是从它的官方培训文档中注意内存开销 部分它清楚地说:枚举通常需要内存是静态常量的两倍多.你应该严格避免在 Android 上使用枚举."这仍然有效吗?(比如在 1.6 之后的 Java 版本中)

With a bit of research I ended up in confusion. From this question "Avoid Enums Where You Only Need Ints" removed from Android's performance tips? it's clear that Google has removed "Avoid enums" from its performance tips, but from it's official training docs Be aware of memory overhead section it clearly says: "Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android." Is this still holds good? (say in Java versions after 1.6)

我观察到的另一个问题是使用 Bundleintents 发送 enums 我应该通过序列化发送它们(即 putSerializable(),我认为与原始的 putString() 方法相比,这是一个昂贵的操作,尽管 enums 是免费提供的.

One more issue that I observed is to send enums across intents using Bundle I should send them by serializing (i.e putSerializable(), that I think an expensive operation compared to primitive putString() method, eventhough enums provides it for free).

有人可以澄清一下哪一种是在 Android 中表示相同内容的最佳方式吗?我应该严格避免在 Android 上使用 enums 吗?

Can someone please clarify which one is the best way to represent the same in Android? Should I strictly avoid using enums on Android?

推荐答案

在需要其功能时使用 enum.不要严格避免它.

Use enum when you need its features. Don't avoid it strictly.

Java 枚举功能更强大,但如果您不需要它的功能,请使用常量,它们占用的空间更少,而且它们本身可以是原始的.

Java enum is more powerful, but if you don't need its features, use constants, they occupy less space and they can be primitive itself.

  • 类型检查 - 您可以只接受列出的值,并且它们不是连续的(见下文我在此处所说的连续)
  • 方法重载 - 每个枚举常量都有自己的方法实现

  • type checking - you can accept only listed values, and they are not continuous (see below what I call continuous here)
  • method overloading - every enum constant has its own implementation of a method

public enum UnitConverter{
    METERS{
        @Override
        public double toMiles(final double meters){
            return meters * 0.00062137D;
        }

        @Override
        public double toMeters(final double meters){
            return meters;
        }
    },
    MILES{
        @Override
        public double toMiles(final double miles){
            return miles;
        }

        @Override
        public double toMeters(final double miles){
            return miles / 0.00062137D;
        }
    };

    public abstract double toMiles(double unit);
    public abstract double toMeters(double unit);
}

  • 更多数据 - 您的一个常量包含多个不能放入一个变量中的信息

  • more data - your one constant contains more than one information that cannot be put in one variable

    • 您可以接受一种类型的所有值,而您的常量仅包含这些最常用的值
    • 您可以接受连续数据

    • you can accept all values of one type, and your constants contain only these most used
    • you can accept continuous data

    public class Month{
        public static final int JANUARY = 1;
        public static final int FEBRUARY = 2;
        public static final int MARCH = 3;
        ...
    
        public static String getName(final int month){
            if(month <= 0 || month > 12){
                throw new IllegalArgumentException("Invalid month number: " + month);
            }
    
            ...
        }
    }
    

  • 用于名称(如您的示例中)
  • 对于真正不需要枚举的其他一切
    • 对枚举常量的单个引用占用 4 个字节
    • 每个枚举常量占用的空间是其字段大小的总和,对齐到 8 个字节 + 对象的开销
    • 枚举类本身占用了一些空间
    • a single reference to an enum constant occupies 4 bytes
    • every enum constant occupies space that is a sum of its fields' sizes aligned to 8 bytes + overhead of the object
    • the enum class itself occupies some space
    • 常量没有引用,所以它是纯数据(即使它是引用,枚举实例也将是对另一个引用的引用)
    • 常量可以添加到现有的类 - 没有必要添加另一个类
    • 常量可以被内联;它带来了扩展的编译时功能(例如空检查、查找死代码等)

    这篇关于我应该严格避免在 Android 上使用枚举吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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