#Prototype ``` // 不好的做法 monster ghost demon sorcerer class Spawner { public: virtual ~Spawner() {} virtual Monster* spawnMonster() = 0; }; class GhostSpawner : public Spawner { public: virtual Monster* spawnMonster() { return new Ghost(); } }; ``` 原型可以spawn出一个类似自己的物件(克隆) ``` class Monster { public: virtual ~Monster() {} virtual Monster* clone() = 0; // Other stuff... }; class Ghost : public Monster { public: Ghost(int health, int speed) : health_(health), speed_(speed) {} virtual Monster* clone() { return new Ghost(health_, speed_); } private: int health_; int speed_; }; // 这样只需要一个spawn类 class Spawner { public: Spawner(Monster* prototype) : prototype_(prototype) {} Monster* spawnMonster() { return prototype_->clone(); } private: Monster* prototype_; }; Monster* ghostPrototype = new Ghost(15, 3); Spawner* ghostSpawner = new Spawner(ghostPrototype); //template Spawner* ghostSpawner = new SpawnerFor<Ghost>(); ```