您的位置:首页 > 其它

Design Patterns - Decorator - Example 2

2012-06-07 23:08 218 查看
#include <iostream>
#include <string>

using namespace std;

class AbstractStuff {
  public:
    virtual string show() {
      return "Unknown Stuff";
    }

  protected:
    string discription;
};

class BenzCar : public AbstractStuff {
  public:
    BenzCar() {
      discription = "Benz";
    }

    string show() {
      return discription;
    }
};

class OtherStuff : public AbstractStuff {
  public:
    virtual string show() = 0;

  protected:
    AbstractStuff *stuff;
};

class Wheel : public OtherStuff {
  public:
    Wheel(AbstractStuff *stuff) {
      this->stuff = stuff;
    }

    string show() {
      return stuff->show() + " + wheel";
    }
};

int main(int argc, char **argv) {
  AbstractStuff *car = new BenzCar();
  car = new Wheel(car);
  cout << car->show() << endl;
  return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: