Created
March 4, 2022 03:15
-
-
Save yknishidate/d0f4138298c1424d04d7a95d8aca56bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Monster | |
{ | |
public: | |
virtual void attack() = 0; | |
}; | |
class Ghost : public Monster | |
{ | |
public: | |
void attack() override { std::cout << "Ghost attacked!" << std::endl; } | |
}; | |
class Demon : public Monster | |
{ | |
public: | |
void attack() override { std::cout << "Demon attacked!" << std::endl; } | |
}; | |
class Spawner | |
{ | |
public: | |
virtual ~Spawner() {} | |
virtual Monster *spawnMonster() = 0; | |
}; | |
template <class T> | |
class SpawnerFor : public Spawner | |
{ | |
public: | |
Monster *spawnMonster() override { return new T(); } | |
}; | |
int main() | |
{ | |
Spawner *ghostSpawner = new SpawnerFor<Ghost>(); | |
Spawner *demonSpawner = new SpawnerFor<Demon>(); | |
Monster *ghost = ghostSpawner->spawnMonster(); | |
Monster *demon = demonSpawner->spawnMonster(); | |
ghost->attack(); | |
demon->attack(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Game Programming Patterns