where to place some information concerning a animal category

T

Tony Johansson

Hello

I have a class hierarcy in this way.
The super class is an abstract class Animal and derived fom that is Bird,
Mammal, Insect, Marine and Reptile all abstract.
Derived from these Bird, Mammal, Insect, Marine and Reptile is the specific
classes like cat, dog, snake, gnat, stoneFish and the like.

Now if I want to add information for each category(Bird, Mammal, Insect,
Marine and Reptile ) for example wingspan for the bird category and number
of teeth for the mammal and so on where is it most suitable to place such
informatiion.

I have done in the following way. Place this abstract CategoryInfo method in
the abstract animal class and then implement this CategoryInfo method in
each of the five animal category classes Bird, Mammal, Insect, Marine and
Reptile
public abstract string CategoryInfo { get; set; }

But it's also possible to place this CategoryInfo direct in the animal
class. So does anyone have an opinoin which is the best way to do ?

Can you also motivate why it best to place it there ?

//Tony
 
A

Arne Vajhøj

I have a class hierarcy in this way.
The super class is an abstract class Animal and derived fom that is Bird,
Mammal, Insect, Marine and Reptile all abstract.
Derived from these Bird, Mammal, Insect, Marine and Reptile is the specific
classes like cat, dog, snake, gnat, stoneFish and the like.

Now if I want to add information for each category(Bird, Mammal, Insect,
Marine and Reptile ) for example wingspan for the bird category and number
of teeth for the mammal and so on where is it most suitable to place such
informatiion.

I have done in the following way. Place this abstract CategoryInfo method in
the abstract animal class and then implement this CategoryInfo method in
each of the five animal category classes Bird, Mammal, Insect, Marine and
Reptile
public abstract string CategoryInfo { get; set; }

But it's also possible to place this CategoryInfo direct in the animal
class. So does anyone have an opinoin which is the best way to do ?

Can you also motivate why it best to place it there ?

By having an abstract property in the base class
you make it possible to use the property for
all elements in a List<Animal>, which is good.

But the approach is not type safe at all. CategoryInfo
will return complete different information for for
different categories of animals, which is not good.

Furthermore I don't like the name because that implies
it is for the category, while it really would be for
the instances of the categories.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
By having an abstract property in the base class
you make it possible to use the property for
all elements in a List<Animal>, which is good.

But the approach is not type safe at all. CategoryInfo
will return complete different information for for
different categories of animals, which is not good.

Furthermore I don't like the name because that implies
it is for the category, while it really would be for
the instances of the categories.

Arne
If the categoryInfo is always a string value which way do you think is the
best between put the info in each category or in the animal class.

//Tony
 
A

Arne Vajhøj

If the categoryInfo is always a string value which way do you think is the
best between put the info in each category or in the animal class.

There must be a thousand different ways of doing this.

Maybe the example attached below can give you some inspiration.

Arne

====

using System;
using System.Collections.Generic;

namespace E
{
public abstract class Animal
{
private string name;
public Animal(string name)
{
this.name = name;
}
public string Name
{
get
{
return name;
}
}
public abstract string Type { get; }
public abstract string Description { get; }
}
public abstract class Bird : Animal
{
public Bird(string name) : base(name)
{
}
public override string Description
{
get
{
return Name + " is a " + Type + " with max. wingspan of
" + Wingspan;
}
}
public abstract double Wingspan { get; }
}
public class Eagle : Bird
{
public Eagle(string name) : base(name)
{
}
public override string Type
{
get
{
return "Eagle";
}
}
public override double Wingspan
{
get
{
return 2.0;
}
}
}
public abstract class Mammal : Animal
{
public Mammal(string name) : base(name)
{
}
public override string Description
{
get
{
return Name + " is a " + Type + " with max. weight of "
+ Weight + " and max. height of " + Height;
}
}
public abstract double Weight { get; }
public abstract double Height { get; }
}
public class Elephant : Mammal
{
public Elephant(string name) : base(name)
{
}
public override string Type
{
get
{
return "Elephant";
}
}
public override double Weight
{
get
{
return 4000.0;
}
}
public override double Height
{
get
{
return 3.0;
}
}
}
public class Program
{
public static void Main(string[] args)
{
List<Animal> alst = new List<Animal>();
alst.Add(new Eagle("Eddie"));
alst.Add(new Elephant("Dumbo"));
foreach(Animal a in alst)
{
Console.WriteLine(a.Description);
}
Console.ReadKey();
}
}
}
 
A

Arne Vajhøj

On 2/20/2011 2:42 PM, Arne Vajhøj wrote:

<snipped>

Yeah that's right, you spoon feed him with the silver spoon. :)

I am not sure that the code qualifies as silver spoon.

More like stainless steel and plastic from IKEA.

But that can be good enough.

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