April 9, 2012

Conditional Builder Design Pattern

Have you ever wanted to use the Builder design pattern to create a really cool fluent interface for building objects but you wanted to add conditions to the building? The good news is that you can do something like so:


Notice how in this contrived example a ninja only gets the "hide in shadows" skill after reaching 6th level? To handle a conditional builder take a look at the following code:

For a complete sample project check it out on my GitHub account!