A heroic pattern
Tags: java, beginners, design
Published: 2020-06-07 13:41:05

# Intro

A short blog on how you can build a mighty fearless hero!
I did that by using a software design patter, the **Builder Pattern**!

## A mighty hero

Many of us at some point we have played an RPG, like Diablo, lineage, WoW etc.
The hero that you are playing with, usually comes with a bunch of attributes.
Like strength, armor, dexterity and so on, thoughts attributes.
Let's see how we can utilize the **Builder Pattern** to create those mighty heroes!

## A hero builder

A builder is an elegant way of creating an object that have many attributes.
Of course, you can do that with other approaches as well, such as:

- a telescopic constructor (many different constructors with different arguments)
- a Java bean (e.g. a simple pojo with setters)

What is needed for a builder:

1. a class, that includes the attributes
2. a inner static class, this class set's the values to attributes that we want

Those are the basic ingredients of the **Builder Pattern**.

In the Hero the two ingredients are mapped to the following code fragments:

1. the Hero class, that contains the heroic attributes (e.g. name, race, strength, dexterity, intelligence, vitality, armor)

2. the Builder static class, is the heart of the pattern, is used to set the values to the attributes

```java
package heroic_pattern;

public class Hero {
    private final String name;
    private final String race;
    private final int strength;
    private final int dexterity;
    private final int intelligence;
    private final int vitality;
    private final int armor;
    private final int damage;

    public static class Builder {
        // mandatory fields
        private String name;
        private String race;

        // default initial values
        private int strength = 0;
        private int dexterity = 0;
        private int intelligence = 0;
        private int vitality = 0;
        private int armor = 0;
        private int damage = 0;

        public Builder(String name, String race) {
            this.name = name;
            this.race = race;
        }

        public Builder strength(int val) {
            strength = val;
            return this;
        }

        public Builder dexterity(int val) {
            dexterity = val;
            return this;
        }

        public Builder intelligence(int val) {
            intelligence = val;
            return this;
        }

        public Builder vitality(int val) {
            vitality = val;
            return this;
        }

        public Builder armor(int val) {
            armor = val;
            return this;
        }

        public Builder damage(int val) {
            damage = val;
            return this;
        }

        public Hero build() {
            return new Hero(this);
        }

    }

    private Hero(Builder builder) {
        name = builder.name; 
        race = builder.race; 
        strength = builder.strength;
        dexterity = builder.dexterity;
        intelligence = builder.intelligence;
        vitality = builder.vitality;
        armor = builder.armor;
        damage = builder.damage;
    }

    @Override
    public String toString() {
        return "Hero [armor=" + armor + ", damage=" + damage + ", dexterity=" + dexterity + ", intelligence="
                + intelligence + ", name=" + name + ", race=" + race + ", strength=" + strength + ", vitality="
                + vitality + "]";
    }
}
```

Please notice, that the attributes that are mandatory, are constructor arguments in the Builder class (e.g. name, race).
The other attributes are optional and have a default value of 0.

## Let's create some Heroes

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8ol02pyl4th3mfvqoap3.png)

## Jargon bonus!

The builder pattern results in a **fluent API** (e.g. chained methods).