Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

08 Oct 2009

Fluent Builders: More than creating context specifications

Last couple of months i have been using the concept of (Fluent) Builder classes in order to create context specifications and i blogged about the steps i take to design their API. Lately i have realised that this concept has more uses than context specifcation only. Here is an example:

In sokoban a game board can be stored in plain text using the following ‘protocol’:

Level element Character
Wall #
Player @
Player on Goal +
Box $
Box on Goal *
Goal .
Floor (space)

Notice how a Fluent Builder allows us to implement this protocol with some elegant code:

actions.Add('#', aBoard => aBoard.AddFloor().WithWall());
actions.Add('@', aBoard => aBoard.AddFloor().WithPlayer());
actions.Add('+', aBoard => aBoard.AddGoal().WithPlayer());
actions.Add('$', aBoard => aBoard.AddFloor().WithBox());
actions.Add('*', aBoard => aBoard.AddGoal().WithBox());
actions.Add('.', aBoard => aBoard.AddGoal());
actions.Add(' ', aBoard => aBoard.AddFloor());