📘 Scenario:
In the mystical kingdom of Aurelia, every hero follows a sacred and structured ritual before, during, and after a battle:
⚠️ 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.
🖥️ 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.