使用enum建立简单的状态机
Overview The enum in Java is more powerful than many other languages which can lead to surprising uses. In this article, I outline some the individual features of enum in Java, and put them together to form a state machine. Enum for Singleton and Utility class You can use an enum as a Singleton or Utility very simply. enum Singleton { INSTANCE; } enum Utility { ; // no instances } Enum to implement an interface You can also implement an interface in an enum. interface Named { public String name(); public int order(); } enum Planets implements Named { Mercury, Venus, Earth, Mars, Jupiter,