Using Interface for Code Organization

Use Interface to better organize your code

Guowei Lv

2 minute read

Another gem found in the book Thinking In Java. Interface in Java can actually be used as a container for closly related concepts. Using Interface and Enum for Subcategorization This can be best explained using an example. Say we have enums for different types of Foods. But we also want to mark that they are all Food. The code is like: public interface Food { enum Appetizer implements Food { SALAD, SOUP, SPRING_ROLLS; } enum MainCourse implements Food { LASAGNE, BURRITO, PAD_THAI, LENTILS, HUMMOUS, VINDALOO; } enum Dessert implements Food { TIRAMISU, GELATO, BLACK_FOREST_CAKE, FRUIT, CREME_CARAMEL; } enum Coffee implements Food { BLACK_COFFEE, DECAF_COFFEE, ESPRESSO, LATTE, CAPPUCCINO, TEA, HERB_TEA; } } Since all enum classes inherit from the built-in java.

Use Enumset to Replace Bit Flag

How to use EnumSet to replace bit flags

2 minute read

I was reorganizing my bookshelf today and found my Thinking in Java lying there so I decided to skim through it. I was quite surprised how much basic stuff in Java I overlooked or forgot. So I decided to re-read the book and write short blog posts of the things that I think are useful but not too many people are talking about nowadays. Here is the first one.