What is PECS

PECS explained

Guowei Lv

2 minute read

You: Master, I heard this PECS in the JAVA world. But I’m deeply puzzled by it. Master: Do you know what it stands for? You: Producer Extends Consumer Super? Master: In order to understand it, you shall stop thinking about what these words mean, it’s not helpful at all. Just follow my questions. You: Yes, Master. Please start. Master: There are plants, then there are fruits, then there are apples and pears.

A Little Java a Few Patterns

Java done by hardcore Lisp hackers

Guowei Lv

15 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. What is bit flag?