《游戏编程模式》(3)
Chatper 5 原型模式 核心思想是一个对象可以生成与自身相似的其他对象。 基类Monster,有一个抽象方法clone: 1 class Monster 2 { 3 public: 4 5 virtual ~Monster() {} 6 virtual Monster* clone() = 0; 7 8 // Other stuff... 9 }; 子类的clone实现: 1 class Ghost : public Monster { 2 public: 3 4 Ghost(int health, int speed) 5 : health_(health), 6 speed_(speed) 7 {} 8 9 virtual Monster* clone() 10 { 11 return new Ghost(health_, speed_); 12 } 13 14 private: 15 int health_; 16 int speed_; 17 }; 通用的Spawner类: 1 class Spawner 2 { 3 4 public: 5 Spawner(Monster* prototype) 6 : prototype_(prototype) 7 {} 8 9 Monster* spawnMonster() 10 { 11 return prototype_-