A Little Java a Few Patterns

An interesting example from the book

Guowei Lv

5 minute read

I got the book A Little Java, A Few Patterns not long ago and have been reading it since. The teaching style of the book is refreshing. I do highly recommend this book if you want to learn about design patterns with a functional programming taste. Or if you simply love pizza. The first half of the book is kind of easy for me since I already know Java and a few design patterns.

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.