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

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

模板方法模式.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;
}

相关推荐

  1. C++经典设计模式2 模板方法模式

    2023-12-13 06:32:06       35 阅读
  2. C++经典设计模式22 总结

    2023-12-13 06:32:06       32 阅读
  3. C++经典设计模式1 介绍

    2023-12-13 06:32:06       20 阅读
  4. C++经典设计模式20 访问者模式

    2023-12-13 06:32:06       26 阅读
  5. C++经典设计模式19 职责链模式

    2023-12-13 06:32:06       23 阅读
  6. C++经典设计模式21 解释器模式

    2023-12-13 06:32:06       30 阅读
  7. C++经典设计模式18 备忘录模式

    2023-12-13 06:32:06       37 阅读
  8. C++经典设计模式15 适配器模式

    2023-12-13 06:32:06       29 阅读
  9. C++经典设计模式16 桥接模式

    2023-12-13 06:32:06       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-13 06:32:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-13 06:32:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-13 06:32:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-13 06:32:06       18 阅读

热门阅读

  1. Spark SQL 的partitionBy() 动态分区

    2023-12-13 06:32:06       34 阅读
  2. Python查找列表中不重复的数字

    2023-12-13 06:32:06       44 阅读
  3. 数据库结构

    2023-12-13 06:32:06       35 阅读
  4. IP协议

    IP协议

    2023-12-13 06:32:06      39 阅读