Understanding the Builder Design Pattern
Example 2: Creating Video Game Heroes

You've now seen the classic theoretical example so you understand the responsibilities of each of the classes in the pattern. Now, here’s another example where we identify each of these classes with a specific problem.

Our problem is the representation of different heroes or characters in a video game. We’ll focus on the classic WoW (World of Warcraft) game, in which the heroes are divided into two races: Humans and Orcs. Each of these heroes can have armor , weapon s or different skills depending on whether the hero is a human or orc.

In the event that the builder pattern is not applied, it causes a constructor to be defined in the Hero class with a long list of parameters ( race , armor , skills , etc), which in turn causes logic to be defined in the constructor to decide whether the armor is human or orc. So, with this initial solution, the problem is coupled since any change in the business logic would mean having to rewrite quite a few pieces of code, with hardly any possibility of reuse.

So, the first thing we have to do is stop and think about how the builder pattern helps us solve this problem. So, we focus on showing the UML diagram that solves this problem and we begin to implement it.

Builder pattern applied to the Hero creation problem of a video game.

In this example, we follow the same order as in the previous example and we start with the model or object that we want to build flexibly.

The Hero class defines the race , armor , weapon and skills properties. All these attributes could be objects but to keep this example simple we’ve left them as character strings.

The HeroBuilder interface defines methods for specific builders. Let’s observe look at how the Hero object is configured, little by little, with setArmor , setWeapon , and setSkills . Finally, we have the build method that finishes the configuration of the object and extracts the Hero object.

Once the builder is defined (as an abstract class or interface) we must build the two specific builders that our problem requires: HumanHeroBuilder and OrcHeroBuilder . In the demo code we have completed with a different string according to each builder. It’s important to note that the build method of each of the builders returns the built object ( Hero ) and resets the state of the object, so it can build another one.

The last element of the pattern is the Hero-Director class that allows you to store configurations repeated throughout the code. In our example, we created three Hero creation setups. For example, the createHero method builds a complete hero — that is, it assigns armor, abilities, and weapons. We also create a hero without any equipment with the createHeroBasic method. Finally, to illustrate another configuration, the createHeroWithArmor method is defined, which returns a hero for whom only the armor has been assigned.
['Carlos Caballero']
['Builder Pattern', 'JavaScript', 'Typescript', 'Design Patterns', 'Programming']