vendredi 1 décembre 2017

How to design the config class for creating objects from a family of classes?

I am designing a library that creates a family of classes and manages it. I need to design config class that is used to instantiate instances from the classes. This config class is just config information.

what is the best way to design the config classes? Should the config class contain the superset of information required to create objects of any class in the family? Or, should there be a hierarchy of classes mirroring the hierarchy of actual classes.

For a concrete example. Lets say I have a library called animalManager which creates various animals based on user config and manages:

class animal {  // Animal base class
public:
    animal(bool isHerbivore, bool isMammal, bool isNocturnal) :
        mIsHerbivore(isHerbivore), mIsMammal(isMammal), mIsNocturnal(isNocturnal) { }

    virtual ~animal() { }

    // methods that do stuffs..
private:
    bool mIsHerbivore;
    bool mIsMammal;
    bool mIsNocturnal;
};

class snake : public animal {   // Snake animal
public:
    snake(bool isHerbivore, bool isMammal, bool isNocturnal, bool isPoisonous) :
        animal(isHerbivore, isMammal, isNocturnal), mIsPoisonous(isPoisonous) { }

    virtual ~snake() {}
private:
    bool mIsPoisonous;
};

class fish : public animal {
public:
    fish(bool isHerbivore, bool isMammal, bool isNocturnal, int numFins) :
        animal(isHerbivore, isMammal, isNocturnal), mNumFins(numFins) {
    }

    virtual ~fish() {}
private:
    int mNumFins;
};
// Cat, parrot, elephant, so on

class animalConfig { // User settings to create animals
    // What is the best way to design this class (/hierarchy) ?
};

class animalManager
{
public:
    animalManager() {}
    ~animalManager() {}

    animal* createAnimal(animalConfig *settings) {
        // Create and return animal
    }
    // So on
};

Aucun commentaire:

Enregistrer un commentaire