← Back to the index page

Template Method

The Template Method is a behavioral design pattern that outlines the overall structure of an algorithm in a superclass while allowing subclasses to modify certain steps without altering the algorithm’s core structure.

Template Method Scheme

The template method is one of the simplest structural design patterns. Most probably you have already used it, without even knowing it.

HeroCreator class

This is the template for all classes. Methods setRace and setProficiency are abstract and will be implemented in subclasses.

---@class HeroCreator
local HeroCreator = {}
HeroCreator.__index = HeroCreator

---@return HeroCreator
function HeroCreator:create()
    self:setRace()
    self:setProficiency()
    return setmetatable(self, { __index = self })
end

return HeroCreator

HumanWarrior and ElfMage classes

These classes are subclasses of HeroCreator which is the template for them.

---@class HumanWarrior
local HumanWarrior = {
    create = HeroCreator.create,
}

---@return HumanWarrior
function HumanWarrior:new()
    local t = {}
    setmetatable(t, { __index = HumanWarrior })
    setmetatable(self, { __index = HeroCreator })
    return t
end

function HumanWarrior:setRace()
    self.race = "human"
end

function HumanWarrior:setProficiency()
    self.proficiency = "warrior"
end

return HumanWarrior
---@class ElfMage
local ElfMage = {
    create = HeroCreator.create,
}

---@return ElfMage
function ElfMage:new()
    local t = {}
    setmetatable(t, { __index = HumanWarrior })
    setmetatable(self, { __index = HeroCreator })
    return t
end

function ElfMage:setRace()
    self.race = "elf"
end

function ElfMage:setProficiency()
    self.proficiency = "mage"
end

return ElfMage

Template Method usage

local HumanWarrior = require("HumanWarrior")
local ElfMage = require("ElfMage")

local humanWarrior = HumanWarrior:create()
local elfMage = ElfMage:create()
print(humanWarrior.race, humanWarrior.proficiency) -- human warrior
print(elfMage.race, elfMage.proficiency)           -- elf   mage

Feedback

For feedback, please check the contacts section. Before writing, please specify where you came from and who you are. Sometimes spammers go insane. Thank you in advance for your understanding.

← Back to the index page