Basic decorator pattern

We will begin with a C++ example of the decorator pattern that follows the classic definition as closely as possible. For this example, we will imagine designing a fantasy game that's set in medieval times (true to life, only with dragons and elves and so on). Of course, what are medieval times without fighting? And so, in our game, the player has a choice of units appropriate for his/her side, and they can do battle when called on. Here is the basic Unit class—at least the combat-related part:

class Unit {    public:    Unit(double strength, double armor) : strength_(strength),                                           armor_(armor) {}    virtual bool hit(Unit& target) { return attack() > target.defense(); }    virtual double attack() = 0; virtual double defense() = ...

Get Hands-On Design Patterns with C++ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.