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

查看:89
本文介绍了我应该严格避免在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_LONG View一样。 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 Enums 提供更好,

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

但是,在枚举是有性能问题的> Android ?

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

有了一些研究,我最终陷入困境。从这个问题
避免枚举你所需要的地方从Android的性能提示中删除?很明显, 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)

我观察到的另一个问题是使用 intents > Bundle 我应该通过序列化(即 putSerializable()发送它们,我认为与原始 putString相比是一个昂贵的操作()方法,eventhough 枚举免费提供)

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 中表示相同的最佳方法?我应该严格避免在<$ c上使用枚举 $ c> Android ?

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

推荐答案

使用枚举当你需要它的功能不要避免它严格

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个字节+ 开销 / li>
    • 枚举类本身占用一些空间

    • a single reference to an enum constant occupy 4 bytes
    • every enum constant occupy space that is a sum of its fields' sizes aligned to 8 bytes + overhead of the object
    • the enum class itself occupy some space

    • 一个常量没有引用,所以它是一个纯数据(即使它是一个引用,那么枚举实例将是对另一个引用的引用)

    • 常量可能被添加到现有的类中 - 没有必要添加另一个类

    • 常量可能内联;它带来了扩展的编译时功能(如空检,查找死代码等)。

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

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