Android: enum vs static final ints?

后端 未结 4 1436
闹比i
闹比i 2021-02-01 15:33

What are the advantages (or disadvantages) of having an enum versus having a set of static final ints in Java Android applications? Are there efficienc

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 16:31

    One advantage of ints over enums is in a CLASS FACTORY. The following C# code is not extensible:

    class Factory
    {
        public enum DrawableType {CIRCLE,SQUARE};
        public static Drawable GetInstance(DrawableEnum e)
        {
            if (e == DrawableType.CIRCLE)
            {
                return new Circle();
            }
            else if (e == DrawableType.SQUARE)
            {
                return new Square();
            }
            else
            {
                throw new IndexOutOfRangeException(); // should never get here
            }
        }
    

    I wrote this poor code. Reviewing Design Patterns, the gang of four used an int. I tried to recover here.

提交回复
热议问题