step-builder/README.md
The Step Builder pattern in Java is an advanced technique to create complex objects with clarity and flexibility. It is perfect for scenarios requiring meticulous step-by-step object construction.
Real-world example
Imagine a scenario where you are assembling a custom computer. The process involves several steps: selecting the CPU, choosing the motherboard, adding memory, picking a graphics card, and installing storage. Each step builds on the previous one, gradually creating a fully functional computer. This step-by-step assembly process mirrors the Step Builder design pattern, ensuring that each component is correctly chosen and installed in a structured manner, ultimately resulting in a custom-built computer that meets specific requirements and preferences.
In plain words
The Step Builder pattern constructs complex objects incrementally through a series of defined steps, ensuring clarity and flexibility in the creation process.
Wikipedia says
The Step Builder pattern is a variation of the Builder design pattern, designed to provide a flexible solution for constructing complex objects step-by-step. This pattern is particularly useful when an object requires multiple initialization steps, which can be done incrementally to ensure clarity and flexibility in the creation process.
Sequence diagram
The Step Builder pattern in Java is an extension of the Builder pattern that guides the user through the creation of an object in a step-by-step manner. This pattern improves the user experience by only showing the next step methods available, and not showing the build method until it's the right time to build the object.
Let's consider a Character class that has many attributes such as name, fighterClass, wizardClass, weapon, spell, and abilities.
@Getter
@Setter
public class Character {
private String name;
private String fighterClass;
private String wizardClass;
private String weapon;
private String spell;
private List<String> abilities;
public Character(String name) {
this.name = name;
}
// toString method omitted
}
Creating an instance of this class can be complex due to the number of attributes. This is where the Step Builder pattern comes in handy.
We create a CharacterStepBuilder class that guides the user through the creation of a Character object.
public class CharacterStepBuilder {
// Builder steps and methods...
public static NameStep newBuilder() {
return new Steps();
}
// Steps implementation...
}
The CharacterStepBuilder class defines a series of nested interfaces, each representing a step in the construction process. Each interface declares a method for the next step, guiding the user through the construction of a Character object.
public interface NameStep {
ClassStep name(String name);
}
public interface ClassStep {
WeaponStep fighterClass(String fighterClass);
SpellStep wizardClass(String wizardClass);
}
// More steps...
The Steps class implements all these interfaces and finally builds the Character object.
private static class Steps implements NameStep, ClassStep, WeaponStep, SpellStep, BuildStep {
private String name;
private String fighterClass;
private String wizardClass;
private String weapon;
private String spell;
private List<String> abilities;
// Implement the methods for each step...
@Override
public Character build() {
return new Character(name, fighterClass, wizardClass, weapon, spell, abilities);
}
}
Now, creating a Character object becomes a guided process:
public static void main(String[] args) {
var warrior = CharacterStepBuilder
.newBuilder()
.name("Amberjill")
.fighterClass("Paladin")
.withWeapon("Sword")
.noAbilities()
.build();
LOGGER.info(warrior.toString());
var mage = CharacterStepBuilder
.newBuilder()
.name("Riobard")
.wizardClass("Sorcerer")
.withSpell("Fireball")
.withAbility("Fire Aura")
.withAbility("Teleport")
.noMoreAbilities()
.build();
LOGGER.info(mage.toString());
var thief = CharacterStepBuilder
.newBuilder()
.name("Desmond")
.fighterClass("Rogue")
.noWeapon()
.build();
LOGGER.info(thief.toString());
}
Console output:
12:58:13.887 [main] INFO com.iluwatar.stepbuilder.App -- This is a Paladin named Amberjill armed with a Sword.
12:58:13.889 [main] INFO com.iluwatar.stepbuilder.App -- This is a Sorcerer named Riobard armed with a Fireball and wielding [Fire Aura, Teleport] abilities.
12:58:13.889 [main] INFO com.iluwatar.stepbuilder.App -- This is a Rogue named Desmond armed with a with nothing.
The Step Builder pattern in Java is used
Benefits:
Trade-offs: