Sign in to follow this  

Design question

This topic is 1923 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

Is it smart to design classes for a game using design composition? I think its called composition. The methods used on this page [url="http://gameprogrammingpatterns.com/component.html"]http://gameprogrammi.../component.html[/url]

Share this post


Link to post
Share on other sites
Is it overall a better choice? Does it have it's pro's con's? Is it better for only some games? Just trying to get an idea about composition.

Share this post


Link to post
Share on other sites
All design is a matter of tradeoffs. If your game would make good use of it then it's a good idea. If the game won't, then it might not be.

Share this post


Link to post
Share on other sites
Ohhh wow, haha more than i thought. And if i understood it correctly you were saying the pros for inheritance was when it was being used for polymorphism?

Also would it be a bad idea to mix composition with inheritance? It seems like in there should be a base object for everything just incase you ever do need to group them. Edited by rockstar8577

Share this post


Link to post
Share on other sites
Inheritance almost always uses composition. The inherited class has to be composed (composition) of [i]something[/i], unless it's just functions.

Also, usually the derived class adds more than just the inherited class' variables, so any derived class is almost always inheriting [i]and[/i]? composing (again: unless it's just adding functions).

Example:
[code]//This class is composition only, even though other classes inherit it.
class BaseClass
{
//Composition:
std::string stuff;
int moreStuff;
MyStruct otherStuff;
};

//This class has inheritance AND composition.
class DerivedA : public BaseClass
{
//Composition:
std::string additionalStuff;
};

//Functions don't typically count as composition, so this class is inheritance only.
class DerivedB : public BaseClass
{
void myFunc();
};[/code]

In the code above, BaseClass is only using composition, but not inheritance. DerivedA is using both composition [i]and[/i] inheritance. DerivedB is only using inheritance (since it doesn't add any member variables).

This class uses [i]neither[/i] inheritance nor composition:
[code]class ClassWithFunctionsOnly
{
void myFunc();
void myOtherFunc();
};[/code]
(Arguably, that class would be better off as functions within a namespace instead of a class - but it could legitimately be an abstract base class if one or more functions were virtual)

Yes, the primary pro of inheritance is polymorphism. The cons really start showing up when people go inheritance-crazy (defaulting to inheritance instead of defaulting to composition), and end up creating dozens upon dozens of abstract classes that aren't really needed by their program.
Abstract classes are classes that cannot be used themselves but define the common interface for other classes to use, for polymorphism. One of the goals of programmers is to make most of your code re-usable. This is good. [i]However[/i], in the quest to make code re-usable, many programmers get carried away with creating [i]overly [/i]generic solutions that aren't really needed, and don't bring them closer to completing their project. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

In the big picture, programming happens in layers. Each layer of code helps hone down [i]from the generic[/i] (libraries, engines, and frameworks) to the specific (your project). If the code you are writing is [i]too[/i] generic, it'll just be another layer of code between the libraries and your end-goal, without actually carrying you further towards that goal. When abstract programming goes too far, you end up just creating another framework ontop of whatever you are already using, [i]thinking[/i] you are making your game, but really just making the unneeded framework that you'll eventually actually build your game upon... if you ever complete it, now that you wasted so much time accomplishing the nothing-framework that [i]looks like[/i] something impressive. [img]http://public.gamedev.net//public/style_emoticons/default/dry.png[/img]

There's an important balance between creating generic code that can be reused in other projects and creating specific code that actually furthers your current project. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1349809916' post='4988440']
That article is talking about Component Based Design, not Composition
[/quote]
That'll teach me to actually read rather than giving a quick skim. Thanks for taking the time to give a more detailed explanation! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
So would this be a good way for drawing many objects?

[CODE]
class Drawable
{
int draw()
{
//Code
}
}
int main()
{
List<Drawable> items;

for (int index = 0; index < items.size(); ++index)
{
items.at(index).draw();
}
}
[/CODE]

Share this post


Link to post
Share on other sites
Yep. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

However, "Drawable" sounds like an abstract base class used in inheritance.
If it is a base class, for polymorphism to work you have to use either pointers or references. So your list would be [b]List<Drawable*>[/b], and you'd need to use new and delete to manage the dynamic memory, or preferably: [b]List<some_smart_ptr<Drawable>> [/b]to not have to worry about the memory AND enable polymorphism.

If it isn't an abstract base class, a better name would be "Item" (I'm assuming it's an item, since your list says 'items'). If it's not an item, perhaps "Object" or "GameObject" would make more sense.

Another hint, unrelated to the current topic:
When iterating over an array, and if you're confident that you are staying within 0 to size-1, you can use items[index] instead of items.at(index). The at() function checks bounds (but you already know you are within bounds) and throws an exception if you are out of bounds. The subscript [] operator doesn't do any bounds checking, and so has unpredictable results when you go out of bounds, but is faster when iterating over a container since you already know you are within bounds.
([size=2]And if you are using C++11, there's an even better way which I won't get into here[/size])

Share this post


Link to post
Share on other sites
This was just an example i could think off of the top of my head.

I actually forgot that you can do that for lists and vectors. Also I would enjoy the C++11 way, incase i do move to that standard.

Share this post


Link to post
Share on other sites
In C++11 instead of a normal for-loop:
[code]//Normal for-loop with indexing:
for(size_t index = 0; index < container.size(); index++)
{
DoSomething( container[index] );
}

//Normal for-loop with iterators:
for(Container<Type>::iterator it = container.begin();
it != container.end(); it++)
{
DoSomething( *it );
}
[/code]

First, C++11 includes the keyword 'auto' which greatly reduces the amount of typing, and makes the code cleaner, and makes the code easier to change:
[code]//Normal for-loop with iterators AND using C++11 'auto' keyword:
for(auto it = container.begin(); it != container.end(); it++)
{
DoSomething( *it );
}
[/code]
Or (with std::begin() and std::end() also working for non-container arrays):
[code]for(auto it = std::begin(container); it != std::end(container); it++)
{
DoSomething( *it );
}[/code]

Second, C++11 also introduces a new version of for() loop for when you know you are iterating from begin() to end() without change, and this further reduces the amount of typing, nicely avoids any (visible) iterator dereferencing, and guarantees to the compiler (and anyone reading the code) that you aren't altering the iterator within the for() loop.

This new for-loop we call 'range-for', and it looks like this:
[code]for(const auto &element : container)
{
DoSomething( element );
}[/code]

So in your example:
[code]some_kind_of_container< some_kind_of_smart_ptr<Drawable> > items;

for(const auto &item : items)
{
item->draw();
}[/code]

Share this post


Link to post
Share on other sites
Yes, exactly. Except C++ doesn't have a for-each loop. Microsoft had it as a non-standard extension in Visual Studio that wasn't portable.
for-range [i]is[/i] standard C++, and every compiler that claims to support C++ must support it from now on.

Visual Studio had "for each()", Boost had FOR_EACH() macro, Qt had foreach() macro. Now we have a single consistent and properly portable for-each. "[i]for(element-type : container)[/i]"

Share this post


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