✨ ARCANUM RUNE DIFFICULTY: Apprentice Behavioral

The Strategy of the Chosen Path

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Divination // Tactics
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <!-- The Strategy parameter dictates the sorting and filtering algorithm -->
    <xsl:param name="tactic" select="'aggressive'"/>

    <xsl:template match="/spellbook">
        <selected-spells>
            <xsl:choose>
                <!-- Strategy 1: Aggressive (High power first) -->
                <xsl:when test="$tactic = 'aggressive'">
                    <xsl:apply-templates select="spell">
                        <xsl:sort select="@power" data-type="number" order="descending"/>
                    </xsl:apply-templates>
                </xsl:when>
                
                <!-- Strategy 2: Defensive (High shield first) -->
                <xsl:when test="$tactic = 'defensive'">
                    <xsl:apply-templates select="spell">
                        <xsl:sort select="@shield" data-type="number" order="descending"/>
                    </xsl:apply-templates>
                </xsl:when>
                
                <!-- Strategy 3: Balanced -->
                <xsl:otherwise>
                    <xsl:apply-templates select="spell">
                        <xsl:sort select="@mana-cost" data-type="number" order="ascending"/>
                    </xsl:apply-templates>
                </xsl:otherwise>
            </xsl:choose>
        </selected-spells>
    </xsl:template>

    <xsl:template match="spell">
        <prepared-spell name="{@name}"/>
    </xsl:template>
</xsl:stylesheet>

A mage entering battle must adapt their tactics instantly. The Strategy pattern in XSLT uses an external xsl:param to swap out algorithms at runtime. Without changing the underlying template structure that renders the spell, the overarching logic governing which spells are chosen and in what order they are processed is seamlessly swapped out based on the injected strategy.

Runic Tags: #xslt #design-patterns #behavioral #strategy #parameters #sorting