Template Method Design Pattern: Realm Defenders

📘 Scenario:

In the mystical kingdom of Aurelia, every hero follows a sacred and structured ritual before, during, and after a battle:

  1. Prepare: wear armor or channel magical energy
  2. Engage: strike or cast spells against foes
  3. Defend: block attacks or raise magical shields
  4. Celebrate: rejoice or reflect after victory
Although all heroes follow the same ritual sequence, different types of heroes (Warrior, Mage, Archer) customize how they perform each step.

⚠️ Problem:

💡 Template Method Solution:

The base Hero class defines the skeleton of the ritual as a final method, calling abstract or overridable steps like prepare(), engage(), etc. Subclasses like Warrior, Mage, and Archer only override the specific steps that differ, ensuring consistent structure while allowing customization.

Template Method Pattern UML

🖥️ Output:

=== Warrior’s Ritual ===
[Warrior] Donning heavy armor and readying sword.
[Warrior] Charges forward with a battle cry!
[Warrior] Raises shield to block incoming blows.
[Warrior] Roars victoriously and pounds chest.

=== Mage’s Ritual ===
[Mage] Chanting incantations and gathering mana.
[Mage] Hurls a blazing fireball at the enemy!
[Mage] Conjures a shimmering magical barrier.

=== Archer’s Ritual ===
[Archer] Docking arrow and adjusting quiver.
[Archer] Fires a volley of arrows with deadly precision.
[Archer] Dodges and keeps distance from foes.
[Archer] Salutes enemy before vanishing into the trees.
  
🔗 View Code on GitHub

⚔️ Hero Battle Rituals

Warrior

Warrior

Mage

Mage

Archer

Archer

📝 Ritual Log will appear here...