Structured Fluent Interfaces

The last post showed how a fluent interface can be used to construct structured data. If data is seen as a tree, it can be described entirely in depth first fashion, some method calls descending in a branch, and some others going back to the parent node. When reading the code, however, it may become …

Key-Value Type Correlation, Generic Version

In my previous post I explored how we can have the type of a map value be correlated with the type of its key. We reached a design where a class contains a Level Three implementation, with Level One client code.This is all well and good, but in a real project this design will probably …

Enforcing State Transitions

To access some service we are using a third-party library with an interface that looks like this: [java]public class Session { public void connect(); public Answer ask(Question question); public void disconnect(); }[/java] The contract specified in the documentation specifies that: The first method invoked on the object may only be connect. The connect method may …

Maps with Variable Value Types

About Maps and Generics Since Java introduced Generics, Maps became a bit safer to use. Before generics, this code would pass compilation without issues: [java]Map fruitColours = new HashMap(); fruitColours.put(Fruit.banana, “blue”); /* In a method somewhere else */ if (fruitColour.get(“banana”) == Colour.blue) { throw new IllegalStateException(“Alien blue bananas not supported”); }[/java]