Table of Contents
What is the Builder Pattern?
The definition of the Builder Pattern from both Design Patterns: Elements of Reusable Object-Oriented Software and Head First Design Patterns: A Brain-Friendly Guide is
The Builder Pattern allows you to encapsulate the construction of a product and allow it to be constructed in steps.
Builder Pattern Class Diagram Explained
The class diagram of the Builder Pattern is pretty straight forward. There is a ConcreteBuilder that is responsible for encapsulating the construction of a Product object and providing an interface for clients to retrieve the Product. Additionally, the ConcreteBuilder implements the abstract Builder interface. Through the Builder interface, the Director object constructs the Product. The Product can typically be described as a complex or composite object that provides interfaces to construct or assemble it’s parts.
Benefits of the Builder Pattern
There are a few benefits of the Builder Pattern.
- The Product’s internal representation is hidden by the Builder interface, therefore it can easily be changed by implementing a new ConcreteBuilder.
- The construction of the Product and its internals are well isolated in the ConcreteBuilder. Meaning, the Director and Clients of the Product don’t know the specifics of how the Product was created and what the Product’s internals are.
- Since the Director has an interface to build different parts of a Product, the Builder Pattern allows more control in the Product’s construction. This is different than other creational patterns that typically build a product all in one call.
Builder Pattern Example using C++
With the NFL Training camps starting up recently, I thought it would be fun to use the Builder Pattern to implement an NFL Scheduler that builds a product GameSchedule as directed by the NFLScheduler. Here is a class diagram of this example.
As I alluded earlier, the NFLScheduler is your Director object. It uses the abstract interface Builder object GameScheduleBuilder. In this example, I have a SportsGameSchedulerBuilder implement this interface (ConcreteBuilder) which constructs a SportsGameSchedule as our Product object. I added an interface to this object called GameSchedule. I did this as I only wanted to expose the Display() method. Some of the parts of SportsGameScheduler that were created by the SportsGameScheduleBuilder include GameDate and Game. The GameScheduleBuilder interface allows these objects to remain hidden from the Director.
Let’s take a look at the implementation.
GameScheduleBuilder.h
SportsGameScheduleBuilder.h
SportsGameScheduleBuilder.cpp
NFLScheduler.h
NFLScheduler.cpp
GameSchedule.h
SportsGameSchedule.h
SportsGameSchedule.cpp
Game.h
Game.cpp
GameDate.h
GameDate.cpp
NFLSchedulerTest.cpp
Here is the output of this program
09/07/17
8:30pm : Kansas City Chiefs at New England Patriots
09/10/17
1:00pm : Buffalo Bills at New York Jets
1:00pm : Philadelphia Eagles at Washington Redskins
1:00pm : Las Vegas Raiders at Tennessee Titans
1:00pm : Tampa Bay Buccaneers at Miami Dolphins
1:00pm : Jacksonville Jaguars at Houston Texans
1:00pm : Arizona Cardinals at Detroit Lions
1:00pm : Atlanta Falcons at Chicago Bears
1:00pm : Baltimore Ravens at Cinncinati Bengals
1:00pm : Cleveland Browns at Pittsburgh Steelers
4:05pm : Indianapolis Colts at Los Angeles Rams
4:25pm : Seattle Seahawks at Green Bay Packers
4:25pm : Carolina Panthers at San Francisco 49ers
8:30pm : New York Giants at Dallas Cowboys
09/11/17
7:10pm : New Orleans Saints at Minnesota Vikings
10:20pm : San Diego Chargers at Denver Broncos
Recommended Resources
Head First Design Patterns: A Brain-Friendly Guide
Design Patterns: Elements of Reusable Object-Oriented Software
In Closing
There you have it. If you followed along, I hope you now have a better understanding of the Builder Pattern. Please feel free to leave comments and feedback. Much appreciated!