samedi 4 avril 2015

Does it make a sense to create an Abstract Factory for factories?

I was googling around this patterns and found that we could create a Factory for Abstract factories and it would really make a sense. To me I make the following example leaned from some C++ book.


Imagine that we have two abstract classes in a game-application: Monster and SuperMonster.


Depedning on the difficult level we also have their implementations: SillyMonster, MediumMonster and HardMonster and so for SuperMonsters.


So now we can create three object factories with the same base class:



class AbstractEnemyFactory
{
public:
virtual Soldier* MakeSoldier() = 0;
virtual Monster* MakeMonster() = 0;
virtual SuperMonster* MakeSuperMonster() = 0;
};

class EasyLevelEnemyFactory : public AbstractEnemyFactory
{
public:
Monster* MakeMonster()
{
return new SillyMonster;
}
SuperMonster* MakeSuperMonster()
{
return new SillySuperMonster;
}
};

//The other two factories


And it seems quite naturally to me if we create AbstractEnemyFactoryFactory which is going to create an appropriate AbstractEnemyFactory implementation depending on what level was choosen by a gamer in runtime.


Question: Does it make sense to encapsulate an ObjectFactory into an AbstractFactory?


I mean we create an Abstract Factory which itself is going to create Object Factories rather than concrete objects which in turn are creating concrete objects. I couldn't find more or less sensible example...


Aucun commentaire:

Enregistrer un commentaire