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. Hekaton是如何影响你数据库的目标恢复时间(RTO)的

    这个周末我发现了SQL Server 2014里Hekaton的一个有趣副作用,很遗憾它会负面影响你数据库的目标恢复时间(Recovery Time Objective,RTO).你已知道,对于每个本 ...

  2. ASP.NET在不同情况下实现单点登陆(SSO)的方法

    第一种:同主域但不同子域之间实现单点登陆 Form验证其实是基于身份cookie的验证.客户登陆后,生成一个包含用户身份信息(包含一个ticket)的cookie,这个cookie的名字就是在web. ...

  3. struts2基础——最简单的一个例子

    学习版本:struts-2.3.15.3 一.导入jar包,可以参考 官方项目 blank. 二.添加配置文件:web.xml struts.xml web.xml: <filter> & ...

  4. eclipse中去掉Js/javsscript报错信息

    1.首先在problem>errors中删除所有js错误: 如下图 2.然后再勾选掉javascript Validator: 3.clean下项目吧,你会发现再也不出现js红叉叉了,哈哈.

  5. Python入门笔记(13):列表解析

    一.列表解析 列表解析来自函数式编程语言(haskell),语法如下: [expr for iter_var in iterable] [expr for iter_var in iterable i ...

  6. CSS3动画基本的转换和过渡

    理论知识不扎实,在一定程度上能体现你解决问题的能力.今天我们拿CSS3动画来说,简单回忆下他的一些基本属性,这些我们在平常应用中会经常用到. 常用动画属性: transform:translate(x ...

  7. SVN 忽略文件但不删除文件

    SVN忽略一些不必要的文件但不删除 如果svn仓库中有一些不希望被别人提交的文件 该如何忽略掉对这个文件的更改但又不删除这个文件呢? 在找了一堆解决方案后得出了如下结论 去除要被忽略文件的版本控制 基 ...

  8. 容器--ArrayList

    一.前言 作为List的重要实现之一,ArrayList已经成了我们编写程序时不可或缺的重要容器之一,面试的时候也经常会被问到,所以,深入理解其实现机制,无论是对于我们正确使用这个类来说,还是准备面试 ...

  9. django配置fcgi参数解释

    manage.py runfcgi minspare=50 maxspare=200 maxchildren=1000 maxrequests=99999 host=127.0.0.1 port=80 ...

  10. WPF下递归生成树形数据绑定到TreeView上

    最终效果图:(用于学习类的效果 图片丑了点,看官莫怪) 新建窗体 然后在前端适当位置插入如下代码: <TreeView x:Name="> <TreeView.ItemTe ...