I have small probalem

T

Tony Johansson

Hello!

Here I have two enum definitions MammalSpecies and BirdSpecies and the two
abstract classes Mammal and Bird.
I think it was a good idea first to put mammal in one enum and Bird in
another enum
but the problem come when I'm about to pass the data enum mammal up to the
base class Animal and when I pass the data enum bird up to the base class
Animal. I just wonder what is the bast way to receive the two different enum
MammalSpecies and BirdSpices in the Animal class. I can define a new enum
called AnimalSpices that would receive the mammal and bird but that seems
like a bad idea. The main point is that I want to store what type of animal
it is like dog, cat, pelican and eagle and so in the base class Animal.
Another solution that might be better is to forget this MammalSpecies and
BirdSpecies but instead put all the animals into one enum AnimalSpices that
I can also use in the base class Animal.

What do you think ?

public enum MammalSpecies
{
Dog, Cat
}

public enum BirdSpecies
{
pelican, eagle
}

abstract class Mammal : Animal
{
const int MammalNumberOflegs = 4;
const double NormalBodyTemerature = 37.0;

private double temperature = NormalBodyTemerature;
private int numberOfLegs = MammalNumberOflegs;
private SkinType skin;
private bool trained;
private bool intelligent;

public Mammal(CategoryType category, MammalSpecies mammal, string
name, string id, double age, GenderType gender,
HousingType housing, EaterType eater, bool aggressive,
List<string> foodMenyList)
: base(category, mammal, name, id, age, gender, housing, eater,
aggressive, foodMenyList)
{}

....
}

abstract class Bird : Animal
{
public Bird(CategoryType category, BirdSpecies bird, string name, string
id, double age, GenderType gender, HousingType housing, EaterType
eater, bool aggressive, List<string> foodMenyList)
: base(category, bird, name, id, age, gender, housing, eater,
aggressive, foodMenyList)
{}
....
}

//Tony
 
T

Tony Johansson

Peter Duniho said:
I think that if your species enum needs to be known by the base Animal
class, then all your species need to be in the same enum. You could cast
to some other type (e.g. int), but then the enum value would be
meaningless in the base class anyway, so why bother putting it in there at
all in that case?

That said, it is possible that the base Animal class does not in fact need
to know the species. After all, species is (essentially) the most
specific classification in biology. You could argue that even the Bird
and Mammal classes don't really need to know the species. That would be
stored in the most-derived class in your hierarchy.

You could also argue that the species shouldn't be an enum at all, and
that your class hierarchy ought to more directly model the biology
classification scheme (kingdom, phylum, class, order, family, genus,
species.each inheriting from the previous), and you would identify the
exact species of an animal simply by examining the actual type of the
animal instance. In that case, the most-derived class would in fact
represent the specific species of the animal.

This is not such an "out there" idea, I think, given that your main base
class is in fact the kingdom and you appear to have used the biological
"class" for your intermediate Bird and Mammal types. And it opens your
design to allow for the easy addition of plants in the future. :)

Pete

So you mean that the abstract base class Animal doesn't need to contain any
field thay say what type of animal it is like cat, dog eagle and so on. In
addition there is as I believe no need for the Animal to have a field
holding the animal category like Mammal, Bird and so on.

Is that correct understood ?

//Tony
 
T

Tony Johansson

Tony Johansson said:
So you mean that the abstract base class Animal doesn't need to contain
any field thay say what type of animal it is like cat, dog eagle and so
on. In addition there is as I believe no need for the Animal to have a
field holding the animal category like Mammal, Bird and so on.

Is that correct understood ?

//Tony

When I click the event handler in the GUI class this method Add in the
AnimalManager class is called that will add a new instance to the animal
collection that is stored in the AnimalManager class.
I mean when I call this Add method I must pass some info that tell what type
of
concreate animal type(cat,dog and so on) that I want to create.
What is the best way to do that is it to pass a enum such as AnimalSpices
containing the actual animal type such as cat or dog or what ever or just
pass a string holding the animal type such as a cat for example. As you can
see in this Add I have no info that tell what type of animaltype that I want
to create.

public int Add(CategoryType category, string name, double age, GenderType
gender, HousingType housing,
EaterType eater, bool aggressive, List<string> foodMenuList)
{
this.category = category;
this.name = name;
this.age = age;
this.gender = gender;
this.housing = housing;
this.eater = eater;;
this.aggressive = aggressive;
this.foodMenuList = foodMenuList;
// int index;

//Check some business rules
if (!ValidateData())
return -1;

//Generate the ID that is unique each animal in the collection
ID = GenerateID();

switch (category)
{
case CategoryType.Mammal:
// IAnimal animalObj = MammalFactory.CreateMammal(animal,
name, ID, age, gender, housing, eater, false, foodMenuList);
// animals.Add((Animal)animalObj);
// index = animals.Count;
break;

//Tony
 
A

Arne Vajhøj

Here I have two enum definitions MammalSpecies and BirdSpecies and the two
abstract classes Mammal and Bird.
I think it was a good idea first to put mammal in one enum and Bird in
another enum
but the problem come when I'm about to pass the data enum mammal up to the
base class Animal and when I pass the data enum bird up to the base class
Animal. I just wonder what is the bast way to receive the two different enum
MammalSpecies and BirdSpices in the Animal class. I can define a new enum
called AnimalSpices that would receive the mammal and bird but that seems
like a bad idea. The main point is that I want to store what type of animal
it is like dog, cat, pelican and eagle and so in the base class Animal.
Another solution that might be better is to forget this MammalSpecies and
BirdSpecies but instead put all the animals into one enum AnimalSpices that
I can also use in the base class Animal.

What do you think ?

Animal should definitely not contain anything specific for sub classes.

A single enum for all Animal's could be used.

But I think an even better idea is not to have that
enum at all.

Because the information about what it is is carried in
the type - the specific sub class.

Switching on an enum and do different things on existing
objects is very bad OO.

You should have abstract methods in the base class and
just call those and let the sub classes implement
appropriately.

Arne
 
A

Arne Vajhøj

When I click the event handler in the GUI class this method Add in the
AnimalManager class is called that will add a new instance to the animal
collection that is stored in the AnimalManager class.
I mean when I call this Add method I must pass some info that tell what type
of
concreate animal type(cat,dog and so on) that I want to create.
What is the best way to do that is it to pass a enum such as AnimalSpices
containing the actual animal type such as cat or dog or what ever or just
pass a string holding the animal type such as a cat for example. As you can
see in this Add I have no info that tell what type of animaltype that I want
to create.

You can pass in an enum for that to decide what class to create.

But that information should not be stored in the object itself
explicit - it is there implicit via the type info.
public int Add(CategoryType category, string name, double age, GenderType
gender, HousingType housing,
EaterType eater, bool aggressive, List<string> foodMenuList)
{
this.category = category;
this.name = name;
this.age = age;
this.gender = gender;
this.housing = housing;
this.eater = eater;;
this.aggressive = aggressive;
this.foodMenuList = foodMenuList;

Get rid of those Animal fields in the AnimalManager.
// int index;

//Check some business rules
if (!ValidateData())
return -1;

//Generate the ID that is unique each animal in the collection
ID = GenerateID();

switch (category)
{
case CategoryType.Mammal:
// IAnimal animalObj = MammalFactory.CreateMammal(animal,
name, ID, age, gender, housing, eater, false, foodMenuList);
// animals.Add((Animal)animalObj);
// index = animals.Count;
break;

You could do that in a single statement, but I think this
usage is OK.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top