开发者

Hierarchical Class design

Let's consider I want to create the following hierarchy:

Animal -> Class (mammal, amphibian, ...) -> Family (Felidae, Canidae, ...) -> Species (cat, tiger, ...).

each sub class dependes on the previous one.

Any suggestion on the best methodology for creating a tree-like class similar to this one?

This is just an example of the problem I have at hands but a solution would help me a lot...

thanks开发者_运维问答

The real problem is this:

I have to parse a message of the following type 0102060800FF.

Firstly, 01 tells me I have a specific type of message. Given that type, i must look for the value 02 in a specific table, and so on...

I want a subhierarchy's possible values filtered by its parent. I'm not sure if i'm being clear enough.

thanks


These days, favor composition over inheritance. Here's an example - needs some more logic checking to make sure you're adding into the proper hierarchy, etc.

public class Class
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Family
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Species
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Animal
{
    private readonly Class @class;
    private readonly Family family;
    private readonly Species species;

    public Animal(Class @class, Family family, Species species)
    {
        if (@class == null)
        {
            throw new ArgumentNullException("@class");
        }

        if (family == null)
        {
            throw new ArgumentNullException("family");
        }

        if (species == null)
        {
            throw new ArgumentNullException("species");
        }

        this.@class = @class;
        this.family = family;
        this.species = species;
        this.@class.AddAnimal(this);
        this.family.AddAnimal(this);
        this.species.AddAnimal(this);
    }

    public Class Class
    {
        get
        {
            return this.@class;
        }
    }


    public Family Family
    {
        get
        {
            return this.family;
        }
    }


    public Species Species
    {
        get
        {
            return this.species;
        }
    }
}


Every class down to the actual animal is an abstract class. Tiger is a concrete class which inherits from abstract class Felidae, from abstract class Mammalia, etc.

If you want to get really fancy, you can use interfaces, like IPredator, IWarmBlooded; define them as capabilities.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜