Advantage

Disadvantage

Enum types

Clarity

Safety

Ease of maintenance.

None extensibility

Typesafe enum pattern(Interfaces to emulate extensible enums)

Extensibility

No good way to enumerate all of the elements of a base type and its extension.

Extensibility would complicate many aspects of the design and implementation.

Implementations cannot be inherited from one enum type to another.

Scenario

Sometimes it is desirable to let the users of an API provide their own operations, effectively extending the set of operations provided by the API.

Compelling use case for extensible enumerated type -- Operation codes/ opcodes.

Usage

  1. Pass a single instance of an "extension enum" anywhere a "base enum" is expected.

    /**

    * Demo for "34 Emulate extensible enums with interfaces".

    */

    package com.effectivejava.EnumAnnotations;

    /**

    * @author Kaibo

    *

    */

    public interface Operation {

    double apply(double x, double y);

    }

    /**

    * Demo for the "34 Emulate extensible enums with interfaces".

    */

    package com.effectivejava.EnumAnnotations;

    import java.util.HashMap;

    import java.util.Map;

    /**

    * @author Kaibo

    *

    */

    public enum BaseOperation implements Operation {

    PLUS("+") {

    public double apply(double x, double y) {

    return x + y;

    }

    },

    MINUS("-") {

    public double apply(double x, double y) {

    return x - y;

    }

    },

    TIMES("*") {

    public double apply(double x, double y) {

    return x * y;

    }

    },

    DIVIDE("/") {

    public double apply(double x, double y) {

    return x / y;

    }

    };

    private final String symbol;

    BaseOperation(String symbol) {

    this.symbol = symbol;

    }

    @Override

    public String toString() {

    return symbol;

    }

    public abstract double apply(double x, double y);

    // Implementing a fromString method on an enum type

    private static final Map<String, BaseOperation> stringToEnum = new HashMap<String, BaseOperation>();

    static { // Initialize map from constant name to enum constant

    for (BaseOperation op : values())

    stringToEnum.put(op.toString(), op);

    }

    // Returns BaseOperation for string, or null if string is invalid

    public static BaseOperation fromString(String symbol) {

    return stringToEnum.get(symbol);

    }

    }

  2. Pass in an entire extension enum type and use its elements in addition to or instead of those of the base type.

/**

* Demo for the "34 Emulate extensible enums with interfaces".

*/

package com.effectivejava.EnumAnnotations;

/**

* @author Kaibo

*

*/

public enum ExtendedOperation implements Operation {

EXP("^") {

public double apply(double x, double y) {

return Math.pow(x, y);

}

},

REMAINDER("%") {

public double apply(double x, double y) {

return x % y;

}

};

private final String symbol;

ExtendedOperation(String symbol) {

this.symbol = symbol;

}

@Override

public String toString() {

return symbol;

}

}

Note

  1. Passing the extended enums between methods.
    1. Bounded type token

      public static void main(String[] args) {

      double x = Double.parseDouble(args[0]);

      double y = Double.parseDouble(args[1]);

      test(ExtendedOperation.class, x, y);

      }

      private static <T extends Enum<T> & Operation> void test(

      Class<T> opSet, double x, double y) {

      for (Operation op : opSet.getEnumConstants())

      System.out.printf("%f %s %f = %f%n", x, op, y, op.apply(x, y));

      }

b. Passing with Collection<? Extends Operation>

public static void main(String[] args) {

double x = Double.parseDouble(args[0]);

double y = Double.parseDouble(args[1]);

test(Arrays.asList(ExtendedOperation.values()), x, y);

}

private static void test(Collection<? extends Operation> opSet, double x, double y) {

for (Operation op : opSet)

System.out.printf("%f %s %f = %f%n", x, op, y, op.apply(x, y));

}

2. The logic to store and retrieve the symbol associated with an operation is duplicated in BasicOperation and ExtendedOperation. In this case it doesn't matter because very little code is duplicated. If there were a larger amount of shared functionality, you could encapsulate it in a helper class or a static helper method to eliminate the code duplication.

Summary

While you cannot write an extensible enum type, you can emulate it by writing an interface to go with a basic enum type that implements the interface.

Effective Java 34 Emulate extensible enums with interfaces的更多相关文章

  1. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  2. 《Effective Java》读书笔记 - 6.枚举和注解

    Chapter 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可 ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. Effective Java 第三版——34. 使用枚举类型替代整型常量

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  6. Effective Java 第三版——38. 使用接口模拟可扩展的枚举

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  7. Effective Java 第三版笔记(目录)

    <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时 ...

  8. [Effective Java]第八章 通用程序设计

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

随机推荐

  1. vuejs入门小demo-搜索大全

    这个demo非常适合入门的同学,不再是简单的todolist.用到的知识点有组件通信,过渡效果,vue-rsource,还有一些基本的vue指令. 先放一张截图: 是不是感觉高端大气上档次呢,演示地址 ...

  2. SpringMVC核心——映射问题

    一.SpringMVC 使用 RequestMapping 来解决映射问题. 二.在学习 RequestMapping 之前,首先来看一张图. 这张图表示的是发送一次 http 请求时,所包含的请求 ...

  3. SQL各种连接查询详解(左连接、右连接..)

    一.交叉连接(cross join) 交叉连接(cross join):有两种,显式的和隐式的,不带on子句,返回的是两表的乘积,也叫笛卡尔积. 例如:下面的语句1和语句2的结果是相同的.语句1:隐式 ...

  4. 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. AccessHelper

    代码: using System; using System.Data; using System.Configuration; using System.Data.OleDb; using ahwi ...

  6. 介绍两种风格的URL

    两种风格的“动态资源”——统一资源定位符(Uniform Resource Lactor,URL) 当前互联网上主要有两种主要风格的URL: 第一种直接在URL中知名文件(比如xxx.php,xxx. ...

  7. C#语言各种集合介绍

    集合,表示可以通过遍历每个元素来访问的一组对象(特别是可使用foreach循环访问)一个集合包括多个元素,即有一个集合类对象和N个元素对象 因为任何集合类都实现了IEnumerable接口,所以任何集 ...

  8. PHP限制HTML内容中图片必须是本站的方法

    本文实例讲述了PHP限制HTML内容中图片必须是本站的方法.分享给大家供大家参考.具体实现方法如下: 1. PHP代码如下: <?php $dom = new DOMDocument; $dom ...

  9. 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink

    [源码下载] 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink 作者:webabcd 介绍重新想象 Windows 8.1 Store A ...

  10. 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件

    [源码下载] 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...