I have wrote the following code:
std::shared_ptr<mtm::Character>
Game::makeCharacter(CharacterType type, Team team, units_t health, units_t ammo, units_t range, units_t power) {
if (health <= 0 || ammo < 0 || range < 0 || power < 0)
{
throw mtm::IllegalArgument();
}
std::shared_ptr<Character> out = nullptr;
if (type == SOLDIER)
{
out = std::shared_ptr<Character>(new mtm::Soldier(team, health, ammo, range, power));
}
if (type == MEDIC)
{
out = std::shared_ptr<Character>(new mtm::Medic(team, health, ammo, range, power));
}
if (type == SNIPER)
{
out = std::shared_ptr<Character>(new mtm::Sniper(team, health, ammo, range, power));
}
return out;
}
As you can see I have some kind of code duplication, what if there are 100 types... I would have to write 100 if statements which doesn't sound the perfect way to do it.
Any suggestions?
Aucun commentaire:
Enregistrer un commentaire