Skip to content

Instantly share code, notes, and snippets.

@mdg
Last active December 20, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdg/6152423 to your computer and use it in GitHub Desktop.
Save mdg/6152423 to your computer and use it in GitHub Desktop.
OO Type Hierarchy
class Animal
{
public:
std::string name;
virtual ~Animal() {}
};
class Dog : public Animal
{
public:
DogSymptoms symptoms;
};
class Cat : public Animal
{
public:
CatSymptoms symptoms;
};
data Dog = Dog { name :: String, symptoms :: DogSymptoms }
data Cat = Cat { name :: String, symptoms :: CatSymptoms }
class Veterinarian
{
private:
DoctorOfVeterinaryMedicineDegree degree;
};
data Veterinarian = Vet { degree :: DoctorOfVeterinaryMedicineDegree }
class TreatableAnimal a where
treat :: Veterinarian -> a -> a
instance TreatableAnimal Dog where
treat vet dog = blah blah
instance TreatableAnimal Cat where
treat vet cat = blah blah
@kyledseever
Copy link

class Symptom
{
public:
    std::string description();
    int severity();
};

class Sniffles : public Symptom
{
public:
    std::string description()
    {
        return std::string("runny nose");
    }

    int severity()
    {
        return 1;
    }
};

// probably a cat-specific symptom, but the hierarchy doesn't dictate that
class Hairball : public Symptom
{
public:
    std::string description()
    {
        return std::string("gross");
    }

    int severity()
    {
        return 3;
    }
};

// almost a dog-specific symptom. by not encapsulating it inside of Dog,
// our application is free to treat strange cats that start to drool.
class Drooling : public Symptom
{
public:
    std::string description()
    {
        return std::string("ruins your clothes");
    }

    int severity()
    {
        return 2;
    }
}

class Treatable
{
public:
    virtual std::vector<Symptom*> getSymptoms() = 0;
};

class Animal : public Treatable
{
public:
    virtual std::vector<Symptom*> getSymptoms()
    {
        return this->symptoms;
    }

private:
    std::vector<Symptom*> symptoms;
};

class Cat : public Animal
{
public:
    void Cat()
    {
        this->symptoms.push(new Hairball);
    }
};

class Dog : public Animal
{
public:
    void Dog()
    {
        this->symptoms.push(new Drooling);
    }
};

class Vet
{
public:
    void treat(Treatable *patient)
    {
        std::vector<Symptom*> symptoms = patient->getSymptoms();

        // vet stuff

        // operate, prescribe medicine, charge too much money, etc.
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment