Jump to content
  • Advertisement
Sign in to follow this  
Thomo

Code reusability

This topic is 3041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm creating a library external to my XNA application, which uses a class structure similar to the 'Animal' examples used in many text books. So for example, I have a set up like this:


public interface IAnimal
{
void Speak();
}

public abstract class Animal : IAnimal
{
protected Object m_mesh;

public abstract void Speak();
}

public LandAnimal : Animal
{
public LandAnimal(Object _mesh)
{
m_mesh = _mesh;
}

public virtual void Walk()
{
}
}

public WaterAnimal : Animal
{
public WaterAnimal(Object _mesh)
{
m_mesh = _mesh;
}

public virtual void Swim()
{
}
}



And in my XNA application, I've included the above library and used it in this way:

public XNAWaterAnimal : WaterAnimal
{
public XNAWaterAnimal(Model _model)
: base(_model)
{
}

public Model GetModel()
{
return m_mesh as Model;
}
}



The whole point of me doing this, is so I can take the original 'Animal' library and use and plug it in to a different rendering/game engine. In other words, I'm trying to make it reusable.

I showed this to a friend and they reported that it seemed unusual and unnecessary. I'd be grateful for some general criticism and ideas to make it better.

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
There's a lot of problems with using inheritance for code re-use. For starters, what happens what you've got a LandWaterAnimal?
With composition instead of inheritance it looks like:
class Voice
{
void Speak();
};
class Feet()
{
void Walk();
};
class Flippers()
{
void Swim();
};

class LandAnimal
{
protected Feet m_feet;
protected Voice m_voice;
};
class WaterAnimal
{
protected Flippers m_flippers;
protected Voice m_voice;
};
class LandWaterAnimal
{
protected Flippers m_flippers;
protected Feet m_feet;
protected Voice m_voice;
};

Also, why do the base classes even need to know about m_mesh? They don't know what type it is, they can't use it, so it seems it should be invisible to them.

Share this post


Link to post
Share on other sites
Thanks for the reply. I really see where you're going with the composition idea.

The reason I have the mesh on the weapon abstraction is because all animals must have a mesh associated with them. It's a contract that any animals created will contain some kind of mesh. Whether that's an XNA Model, or some other represtentation of the actual render object.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!