Robert Larson

  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Builder Pattern using C++ (NFL Scheduler Example)

Builder Pattern using C++ (NFL Scheduler Example)

August 2, 2017 by Robert Larson

Table of Contents

  • What is the Builder Pattern?
  • Builder Pattern Class Diagram Explained
  • Benefits of the Builder Pattern
  • Builder Pattern Example using C++
  • Recommended Resources
  • In Closing
  • Like this:
  • Related

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

Builder Pattern Class Diagram

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.

  1. The Product’s internal representation is hidden by the Builder interface, therefore it can easily be changed by implementing a new ConcreteBuilder.
  2. 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.
  3. 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.

Builder Pattern using C++

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!

Share this:

  • Share on X (Opens in new window) X
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Facebook (Opens in new window) Facebook
  • Email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Posted in: c++, Design Patterns Tagged: #cplusplus, designpatterns
← Bridge Pattern using C++ (Cell Phone Text App Example)
March Madness 2018 – Weighted Random Bracket Generator →
  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Recent Posts

  • March Madness 2019 – Weighted Random Bracket Generator
  • Gifts Ideas for Software Developers 2018-2019
  • March Madness 2018 – Second Chance Weighted Random Bracket Generator
  • March Madness 2018 – Weighted Random Bracket Generator
  • Builder Pattern using C++ (NFL Scheduler Example)

Archives

  • March 2019
  • November 2018
  • March 2018
  • August 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017

Follow me on Twitter

My Tweets

Copyright © 2026 Robert Larson.

Grace WordPress Theme by SiteChurch

%d