
Episode 3 · Season 5
Software Patterns - The Builder
About this episode
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.
More from Develpreneur: Become a Better Developer and Entrepreneur
A handful of people love your MVP. Their suggestions keep coming, and AI helps you turn those requests into working features quickly. The product is growing. Whether it is growing in the right directi…
Your team can generate an interface faster than ever. Meanwhile, customers are still staring at confusing error messages, waiting for pages to load, and trying to figure out what happens after they cl…
After a year of deadlines, long hours, and projects that seemed determined to last forever, Thanksgiving offers a chance to pause and enjoy what all that effort has made possible. In this Thanksgiving…
The holiday is almost here, but your brain is still at work. There is something to finish, something to check, and probably something you could squeeze in before everyone arrives. Closing the laptop s…
Building a successful business requires more than developing a great product. As companies grow, they face new challenges involving customer expectations, operational efficiency, financial decisions,…