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

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

问题描述

我来一起定义了一组相关常量像捆绑键像下面的接口:

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";
}

这为我提供一个更好的方式将相关常数在一起,通过一个静态导入(而不是工具)使用它们。我知道安卓框架也使用常量同样的方式,比如 Toast.LENTH_LONG 查看。 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枚举提供更好更强大的方式来重新present不断。

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

但有一个性能自动问题,使用枚举安卓

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

使用我在混乱结束了一些研究。从这个问题 <一href="http://stackoverflow.com/questions/5143256/why-was-avoid-enums-where-you-only-need-ints-removed-from-androids-performanc">"Avoid枚举如果你只需要整型,从Android的性能提示删除?的很明显,谷歌已删除的从它的性能,避免枚举的提示,但是从它的官方培训文档要注意部分内存开销上面清清楚楚地写着:枚举往往需要超过两倍的内存静态常量。您应严格避免使用Android上枚举。的是这仍然有效? (说在的Java 1.6之后版本)

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)

这是我发现的一个多的问题是发送枚举横渡意图使用捆绑我要通过序列化并将其发送(即 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).

有人能明确哪一个是重新present在安卓同样的最好方法?我应该严格避免使用枚举安卓

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字节+ 对象的开销
    • 枚举类本身占用一些空间
    • 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
    • 在一个恒定的不具有参考所以它是一个纯粹的数据(即使它是一个基准,然后枚举的实例将引用另一个参考)
    • 常量可以添加到现有的类 - 它不需要添加其他类
    • 参数有可能被内联;它带来的扩展编译时功能(如空检查,发现死code等)

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

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