美文网首页C++ 杂记
条款 35:考虑 virtual 函数以外的其他选择(二)

条款 35:考虑 virtual 函数以外的其他选择(二)

作者: 赵者也 | 来源:发表于2017-08-01 09:42 被阅读5次

    Effective C++ 中文版 第三版》读书笔记

    某已知人物健康指数计算函数可以在运行期变更:例如 GameCharacter 可提供一个成员函数 setHealthCalculator,用来替换当前的健康指数计算函数。

    这些计算函数并未特别访问 “即将被计算健康指数” 的那个对象内部成分。例如 defaultHealthCalc 并未访问 EvilBadGuy 的 non-public 成分。如果需要 non-public 信息进行精确计算,就有问题了。唯一能解决 “需要以 non-member 函数访问 class 的 non-public 成分” 的办法就是:弱化 class 的封装。例如 class 可以声明那个 non-member 函数为 friends,或是为其实现的某一部分提供 public 访问函数。利用函数指针替换 virtual 函数,其优点(像是 “每个对象可各自拥有自己的健康计算函数” 和 “可在运行期间改变计算函数”)是足以弥补缺点(例如可能必须降低 GameCharacter 封装性)。

    藉由 tr1::function 完成 Strategy 模式

    我们不再用函数指针,而是用一个类型为 tr1::function 的对象,这样的对象可持有(保存)任何可调用物(callable entity,也就是函数指针、函数对象、或成员数函数指针),只要其签名式兼容于需求端。

    class GameCharacter; 
    int defaultHealthCalc(const GameCharacter& gc); 
    
    class GameCharacter { 
    public: 
        //HealthCalcFunc可以是任何“可调用物”,可被调用并接受 
        //任何兼容于GameCharacter之物,返回任何兼容于int的东西。 
        typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc; 
    
        explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc) 
            : healthFunc(hcf) 
        {} 
    
        int healthValue() const 
        { 
            return healthFunc(*this); 
        } 
        ... 
    private: 
        HealthCalcFunc healthFunc; 
    };
    

    这里我们把 tr::function 的具现体的目标签名式以不同颜色强调出来。那个签名代表的函数是 “接受一个 reference 指向 const GameCharacter,并返回 int”

    std::tr1::function<int (const GameCharacter&)>
    

    所谓兼容,意思是这个可调用物的参数可被隐式转换为 const GameCharacter&,而其返回类型可被隐式转换成 int。

    客户在 “指定健康计算函数” 这件事上有更惊人的弹性:

    short calcHealth(const GameCharacter&); //函数return non-int 
    
    struct HealthCalculator {//为计算健康而设计的函数对象 
        int operator() (const GameCharacter&) const 
        { 
            ... 
        } 
    }; 
    
    class GameLevel { 
    public: 
        float health(const GameCharacter&) const;//成员函数,用于计算健康 
        ... 
    }; 
    
    class EvilBadGuy : public GameCharacter { 
        ... 
    }; 
    
    class EyeCandyCharacter : public GameCharacter { 
        ... 
    };
    
    EvilBadGuy ebg1(calcHealth);//函数 
    EyeCandyCharacter ecc1(HealthCalculator());//函数对象 
    GameLevel currentLevel; 
    ... 
    EvilBadGuy ebg2(std::tr1::bind(&GameLevel::health, currentLevel, _1));//成员函数
    

    GameLevel::health 宣称它接受两个参数,但实际上接受两个参数,因为它也获得一个隐式参数 GameLevel,也就是 this 所指的那个。然而 GameCharacter 的健康计算函数只接受单一参数:GameCharacter。如果我们使用 GameLevel::health 作为 ebg2 的健康计算函数,我们必须以某种方式转换它,使它不再接受两个参数(一个 GameCharacter 和一个 GameLevel),转而接受单一参数(GameCharacter)。于是我们将 currentLevel 绑定为 GameLevel 对象,让它在 “每次 GameLevel::health 被调用以计算 ebg2 的健康” 时被使用。那正是 tr1::bind 的作为。

    古典的 Strategy 模式将健康计算函数做成一个分离的继承体系中的 virtual 成员函数。

    class GameCharacter; 
    
    class HealthCalcFunc { 
        ... 
        virtual int calc(const GameCharacter& gc) const 
        {...} 
        ... 
    }; 
    
    HealthCalcFunc defaultHealthCalc; 
    
    class GameCharacter { 
    public: 
        explicit GameCharacter(HealthCalcFunc* phcf = &defaultHealthCalc) 
            :pHealthCalc(phcf); 
        {} 
    
        int healthValue() const 
        { 
            return pHealthCalc->calc(*this); 
        } 
        ... 
    
    private: 
        HealthCalcFunc* pHealthCalc; 
    };
    

    每一个 GameCharacter 对象都内含一个指针,指向一个来自 HealthCalcFunc 继承体系的对象。

    还可以提供 “将一个既有的健康计算算法纳入使用” 的可能性 —— 只要为 HealthCalcFunc 继承体系添加一个 derived class 即可。

    相关文章

      网友评论

        本文标题:条款 35:考虑 virtual 函数以外的其他选择(二)

        本文链接:https://www.haomeiwen.com/subject/epvllxtx.html