《C++新经典设计模式》之第2章 模板方法模式

《C++新经典设计模式》之第2章 模板方法模式

模板方法模式.cpp
cpp 复制代码
#include <iostream>
#include <memory>
using namespace std;

// 动态绑定,多态,稳定+变化
// 稳定,内部函数调用顺序固定
// 变化,内部具体函数调用由各子类决定

namespace ns1
{
    class Warrior // "战士"类
    {
        int m_life;   // 生命值
        int m_magic;  // 魔法值
        int m_attack; // 攻击力

    public:
        Warrior(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}

    public:
        void JN_Burn() // 技能"燃烧"
        {
            m_life -= 300;
            cout << "Lose 500 HP points for each enemy" << endl;
            cout << "The protagonist loses 300 HP" << endl;
            cout << "Play the special effect of the \"burning\" skill to the players" << endl;
        }
    };
}

namespace ns2
{
    class Fighter // 战斗者父类
    {
    protected:
        int m_life;   // 生命值
        int m_magic;  // 魔法值
        int m_attack; // 攻击力

    public:
        Fighter(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
        virtual ~Fighter() {}
        void JN_Burn() // 技能"燃烧"
        {
            if (canUseJN()) // 如果能使用该技能
            {
                effect_enemy(); // 对敌人产生的影响
                effect_self();  // 对主角自身产生的影响

                play_effect(); // 播放技能"燃烧"的技能特效
            }
        }

    private:
        virtual bool canUseJN() const = 0; // 判断是否能使用技能"燃烧",纯虚函数声明,子类中必须实现

    private:
        virtual void effect_enemy() const {}
        virtual void effect_self() {}

    private:
        void play_effect() const { cout << "Play the special effect of the \"burning\" skill to the players" << endl; }
    };

    class F_Warrior : public Fighter // "战士"类
    {
    public:
        F_Warrior(int life, int magic, int attack) : Fighter(life, magic, attack) {}

    private:
        bool canUseJN() const override { return m_life >= 300; }

    private:
        void effect_enemy() const override
        {
            cout << "Lose 500 HP points for each enemy" << endl;
        }
        void effect_self() override
        {
            cout << "The protagonist loses 300 HP" << endl;
            m_life -= 300;
        }
    };

    class F_Mage : public Fighter // "法师"类
    {
    public:
        F_Mage(int life, int magic, int attack) : Fighter(life, magic, attack) {}

    private:
        bool canUseJN() const override { return m_magic >= 100; }

    private:
        void effect_enemy() const override
        {
            cout << "Lose 650 HP points for each enemy" << endl;
        }
        void effect_self() override
        {
            cout << "The protagonist loses 100 MV" << endl;
            m_magic -= 100;
        }
    };
}

int main()
{
#if 0
    using namespace ns1;
    shared_ptr<Warrior> mroleobj(new Warrior(1000, 0, 200));
    mroleobj->JN_Burn();
#endif

#if 1
    using namespace ns2;
    shared_ptr<Fighter> fighter(new F_Warrior(1000, 0, 200));
    fighter->JN_Burn();
    cout << "-------------------------" << endl;
    fighter.reset(new F_Mage(800, 200, 300));
    fighter->JN_Burn();
#endif

    cout << "Over!\n";
    return 0;
}
相关推荐
ZZZS05162 分钟前
stack栈练习
c++·笔记·学习·算法·动态规划
位东风25 分钟前
【c++学习记录】状态模式,实现一个登陆功能
c++·学习·状态模式
雷羿 LexChien2 小时前
C++内存泄漏排查
开发语言·c++
易元2 小时前
设计模式-模板方法模式
后端·设计模式
嘉小华3 小时前
CMake 完全指南:第一章 - 构建的烦恼 - 为什么需要CMake?
c++
Feliz Da Vida3 小时前
[代码学习] c++ 通过H矩阵快速生成图像对应的mask
c++·学习
花好月圆春祺夏安3 小时前
基于odoo17的设计模式详解---策略模式
设计模式·策略模式
无聊的小坏坏4 小时前
单调栈通关指南:从力扣 84 到力扣 42
c++·算法·leetcode
YOLO大师5 小时前
华为OD机试 2025B卷 - 小明减肥(C++&Python&JAVA&JS&C语言)
c++·python·华为od·华为od机试·华为od2025b卷·华为机试2025b卷·华为od机试2025b卷
看到我,请让我去学习6 小时前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉