The Strategy in AWK
Hot-swap sorting and evaluation algorithms based on dynamic runtime configurations.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Evocation // Algorithm-Swapping
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# Strategy 1: Numeric Comparison
function strat_numeric(a, b) {
return a - b
}
# Strategy 2: Lexical Comparison
function strat_lexical(a, b) {
if (a "" < b "") return -1
if (a "" > b "") return 1
return 0
}
# The Context Executing the Strategy
function compare_elements(strategy, a, b) {
if (strategy == "NUMERIC") return strat_numeric(a, b)
if (strategy == "LEXICAL") return strat_lexical(a, b)
}
BEGIN {
val1 = "10"; val2 = "2"
# Lexical: "10" is less than "2"
print "Lexical Result: " compare_elements("LEXICAL", val1, val2)
# Numeric: 10 is greater than 2
print "Numeric Result: " compare_elements("NUMERIC", val1, val2)
}
The Strategy pattern provides the flexibility to alter behavior at runtime. When sorting or filtering records, instead of hardcoding the comparison type, the algorithm delegates to an interchangeable strategy function selected by the Archmage’s arguments.
Runic Tags:
#awk
#text-processing
#behavioral