In this episode, we look at the Builder pattern.  This one is another creational pattern that helps us construct the class instances for our application.  It is another pattern that could be skipped through brute-force and a wide range of constructors, but I think you will see this is a much better approach.

The Builder Defined

It helps to start with the “Gang of Four” definition and then we will dig into that.

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

This is a pattern that I think we can all easily buy into.  When you have a class that is more than a few values, and it has actual pieces that it is comprised of then the constructor becomes a challenge.  You will either end up with a confusing bunch of constructors or a lot of frustrating side effects.  It is important to note that a Builder is an interface and there will be concrete builders under the covers.  You can see a lot of real-world examples in tools like parsers, readers, and writers.

Applying The Pattern

The Builder is often a sort of meta-class that has internal classes as part of its functionality.  This usually occurs in an application when there is a process to execute that has various implementations.  The parser mentioned above is an excellent example.  You will have some steps along the lines of the list below.

  • Start a block (line or lines)
  • Find end of block
  • Tokenize or Process tags
  • Find subsections
  • List blocks (or iterator of blocks)
  • Move to next block

In our example, think about whether you are parsing a file that is CSV, XML, or fixed-width.  The way you parse each of these is very different with the blocks being defined by an end of line character, XML close tags, or a count of characters.  Then, within each block, the way you parse the data is very different.  If you are building a document reader class, then you probably do not want to load it up with all of the methods required for this.  The Builder allows you to group those methods based on the type of file to be processed.

Java, PHP, C#, etc.

A Builder is an interface.  That makes it easy to implement in any of these languages. You may want to create a base class for your concrete builders as well that has the order of the steps to be processed.  I have seen this handled a couple of different ways so your goals should drive your approach.  The example we looked at is one that is best approached with the most complex solution used as a guideline for the core interface.  Then you can have a success value returned for the steps that are not needed.

As a creation pattern, the Builder is very similar to the others in this group.  That can prompt the question of when to use which.  There is no hard and fast rule for which to use when.  However, you will often find that you start with a factory for simpler classes and move towards builders for the more complex ones.

 

Rob Broadhead

Rob is a founder of, and frequent contributor to, Develpreneur. This includes the Building Better Developers podcast. He is also a lifetime learner as a developer, designer, and manager of software solutions. Rob is the founder of RB Consulting and has managed to author a book about his family experiences and a few about becoming a better developer. In his free time, he stays busy raising five children (although they have grown into adults). When he has a chance to breathe, he is on the ice playing hockey to relax or working on his ballroom dance skills.

Leave a Reply