Sign in to follow this  
mindrot

very abstract object design question

Recommended Posts

sometimes i realise how much of a coding newbie i really am. i can do a lot of really advanced programs, but then i forget something as simple as this...honestly, its kind of refreshing! anyways i would like to have my object structure layout designed in a certain way and would like to have other member's opinions on it, to see if it will work. but honestly i dont know to impliment what i want to do so ill just describe it here. i want to have an image class there are two types of images. mobile and nonmobile mobile simply means it adds additional functionality to the image that allows it to smoothly relocate at controlled speeds, etc. lacking this information effectivly makes the image static. so thus far i have the two classes class image //static image functionality; load, delete, relocate(hard relocate (instant teleportation)) and class mobile : public image // functionality that allows for smooth movement in addition to these there are two sub categories, both of which need to be accessible to either static or mobile classes. these are class animated : inherit mobile or image? or what? animated classes add the ability for an image to change frames at certain rates to animate the sprite like a cartoon or flip-book. class nonanimated lack of the animation functionality is default. so while this is a category it doesnt really need a class so i have the following structure image mobile:public image animated:public image/mobile the question - and reason for this post is. animated needs to work with EITHER image or mobile it would be possible to have a non locomotive iamge that had multiple animated frames - for example, a tree. i use C++ how would i impliment this programatically?

Share this post


Link to post
Share on other sites
diagram

image
- animated
- mobile
- animated



hypen denotes child object of object directly above it.
animated is one class - and contains the same functionality.
but its needs to be made availible to both the base class image
and to images' child - mobile

again - sorry for overlooking what is probably very obvious

Share this post


Link to post
Share on other sites
A first solution is to create an "animation" template which simply adds relevant functions and members on top of one or more underlying image base classes of your choice, and then derive a smooth image class.


template<typename image_t>
class Animation : public image_t
{
// Add here animation-relevant functions, which may use
// protected or public members of the base Image
};


A second solution, assuming that the interface of all classes (regardless of smoothing or animation) is the same, is to use strategies. You basically have a position strategy (either smooth or normal), and an animation strategy (either multiple frames, or a single frame), and each image gets two strategies specified as template arguments:


class ImageBase { /* Pure virtuals */ };
template<typename anim_t,typename pos_t>
class Image : public ImageBase
{
anim_t animation_strategy;
pos_t position_strategy;
// Implement pure virtuals here.
// Whenever one depends on a strategy, use a member function of
// that strategy to do the work.
};

Share this post


Link to post
Share on other sites
Edit: I agree with ToohrVyk.

Just a couple of ideas:

a) You could make animated a template class which accepts as its parameter the image type ( mobile or image ).
animated<mobile> mobile_animate;
animated<image> image_animate;
You could inherit from the parameter type or contain it.

b) Contain an image pointer in your animated class instead of inheriting from image or mobile and calls its virtual function/s.

Share this post


Link to post
Share on other sites
thank you guys very much!!!
i dont know if its the work or the lack of coffee
but i didnt even think of useing templates
DOH!

thanks again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this