行为型模式——模板方法模式

模板方法设计模式适用于以下情况:

• 一次性实现一个算法的不变部分,并将可变的行为留给子类来实现;

• 各子类中公共的行为应被提取出来并集中到一个公共父类中以避免代码重复;

• 控制子类扩展,模板方法只在特定点调用钩子操作,这样就只允许在这些点进行扩展;

下面是一个模板方法模式的示例程序:

cpp 复制代码
#include <iostream>
#include <string>
#include <memory>

// =================================================================
// Step 1: THE ABSTRACT CLASS WITH THE TEMPLATE METHOD
// =================================================================
class GameCharacter {
public:
    virtual ~GameCharacter() = default;

    // THE TEMPLATE METHOD
    // This defines the strict, unchangeable skeleton/sequence of a turn.
    // Notice it is NOT virtual. Subclasses cannot change the step order.
    void takeTurn() {
        std::cout << "\n--- Starting Character Turn Sequence ---\n";
        findTarget();
        attack();
        endTurn(); // A fixed step shared by all characters
        std::cout << "--- Turn Completed Safely ---\n";
    }

protected:
    // Step 1: Subclasses must implement their own custom targeting logic
    virtual void findTarget() = 0;

    // Step 2: Subclasses must implement their own unique combat logic
    virtual void attack() = 0;

    // Step 3: A default implementation shared by all characters.
    // Subclasses can optionally override this if they want to, but don't have to.
    virtual void endTurn() {
        std::cout << "[Default System] Regenerating +10 Mana/Stamina points.\n";
    }
};


// =================================================================
// Step 2: CONCRETE SUBCLASSES (Customizing specific steps)
// =================================================================

// Character Type A: An Orc warrior
class Orc : public GameCharacter {
protected:
    void findTarget() override {
        std::cout << "[Orc Engine] Scanning for the closest enemy standing nearby.\n";
    }

    void attack() override {
        std::cout << "[Orc Engine] Smashing target violently with a heavy iron axe!\n";
    }
};

// Character Type B: A Magic Mage wizard
class Mage : public GameCharacter {
protected:
    void findTarget() override {
        std::cout << "[Mage Engine] Locating the enemy with the lowest total health points.\n";
    }

    void attack() override {
        std::cout << "[Mage Engine] Casting a brilliant fireball spell from a distance!\n";
    }

    // Overriding the default optional hook step to add unique rules
    void endTurn() override {
        GameCharacter::endTurn(); // Call the base behavior first
        std::cout << "[Mage Bonus] Teleporting 5 meters backward into a safe zone.\n";
    }
};


// =================================================================
// Step 3: CLIENT CODE
// =================================================================
int main() {
    std::cout << "=== Game Battle Round Starting ===\n";

    // 1. Create our custom polymorphic game characters
    std::unique_ptr<GameCharacter> player1 = std::make_unique<Orc>();
    std::unique_ptr<GameCharacter> player2 = std::make_unique<Mage>();

    // 2. Execute their turns. 
    // They run the exact same structural template sequence but execute custom code.
    std::cout << "\n>>> Orc Player Turn:";
    player1->takeTurn();

    std::cout << "\n>>> Mage Player Turn:";
    player2->takeTurn();

    return 0;
}

程序运行结果如下:

shell 复制代码
$ g++ -o main main.cpp
$ ./main
=== Game Battle Round Starting ===

>>> Orc Player Turn:
--- Starting Character Turn Sequence ---
[Orc Engine] Scanning for the closest enemy standing nearby.
[Orc Engine] Smashing target violently with a heavy iron axe!
[Default System] Regenerating +10 Mana/Stamina points.
--- Turn Completed Safely ---

>>> Mage Player Turn:
--- Starting Character Turn Sequence ---
[Mage Engine] Locating the enemy with the lowest total health points.
[Mage Engine] Casting a brilliant fireball spell from a distance!
[Default System] Regenerating +10 Mana/Stamina points.
[Mage Bonus] Teleporting 5 meters backward into a safe zone.
--- Turn Completed Safely ---
相关推荐
是2的10次方啊1 小时前
设计模式与生活⑲:投影连不上,别急着换投影仪
设计模式
David猪大卫17 小时前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
HPtiger17 小时前
C++ 入门笔记:从 C 到 C++ 的核心过渡(命名空间、输入输出、缺省参数、重载、引用、内联)
c语言·c++·学习
努力努力再努力wz20 小时前
【Lua 进阶系列】从 C++ 对象到 Lua 变量:一文理解 userdata、Lua 栈与元表绑定机制
c++·junit·lua
慧都产品智能化21 小时前
Qt 表格升级攻略:复用已有编辑器,减少重复开发
c++·qt·编辑器·qt6.3
ShineWinsu21 小时前
对于Qt:QTableWidget常见属性以及QTableWidgetItem类的解析
开发语言·c++·qt·gui·客户端·表格·qtablewidget
cjz38991 天前
规则树:把一百行if-else拆成一棵树
设计模式
ShineWinsu1 天前
对于Qt:QListWidget常见属性以及QListWidgetItem类的解析
开发语言·c++·qt·客户端·列表·qlistwidget·qlistwidgetitem
坤坤子吖1 天前
Linux网络编程:自定义应用层协议与 JSON 序列化
linux·服务器·网络·c++·笔记·json
沫璃染墨1 天前
从零入门计算机网络系列(十八):TCP连接管理详解——从三次握手与四次挥手到TIME_WAIT机制
运维·服务器·网络·c++·网络协议·tcp/ip·tcp