Archived

This topic is now archived and is closed to further replies.

dave

Why Not Just Use Singleton Classes?

Recommended Posts

dave    2187
Hi, What reasons are there for me learning to use advanced classes and methods like Polymorphism and Inheritance. What advantages are there when programming them along side DX compared to just using Singletons? regards, Ace

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Why have pants when shirts exist? It is because they do different things. Every programming construct exists because someone thought that it would be useful to him. Will it be useful to you? Maybe not, maybe you are not working on a similar problem or in a similar setting.

Share this post


Link to post
Share on other sites
dave    2187
All im asking really is why do the tutorials on here use more complex classes than i do. what is it about them that could be more useful to me than singletons.

regards,

ace

Share this post


Link to post
Share on other sites
markr    1692
quote:
Original post by ace_lovegrove
What reasons are there for me learning to use advanced classes and methods like Polymorphism and Inheritance....



Polymorphism is just the RIGHT way to do things.

Basically, it means you can write code which handles generic objects without worrying about their exact behaviour or state details.

If you have a class Player and another class Monster - they have some behaviour and state in common. For example they might both have a hitpoints property, and perhaps a move() and draw() method.

Your manager routine doesn''t need to worry about whether a specific object is an instance of Player, Monster, or something else that you hadn''t yet invented when you wrote them, it will just do the right thing when it asks them to move, draw, or their hitpoints.

You will need to write new code in fewer places when you add a new type of object to your game. Code for handling that class will all be in one place.

Mark

Share this post


Link to post
Share on other sites
jollyjeffers    1570
with respect to abstraction, polymorphism and inheritance - they are almost always used for one (or both) of two reasons:

1. It makes for a simpler, more concise solution to the problem

2. It''s extensible, allowing for added functionality without any major changes to the bulk of the existing code.

Short of writing a small essay I doubt that anyone here can answer your problem. I suggest you find some tutorials on OOP (Object Oriented Programming) or some of the universities CS lecture slides on OOP/PRG type modules.

hth
Jack

Share this post


Link to post
Share on other sites
Palidine    1315
a good reason for polymorphism is so you can have all kinds of objects like Enemy, Monster, Bullet, Player, etc and do things like have a list of pointers to them all in one place to do specific common things. for intsance all of them perhaps extend a PhysicsBase object so that they physics engine can just keep a list of pointers to them (PhysicsBase * physObjs). then it can just iterate through them and call the common collision functions or something. at the same time your graphics engine can have a list of something like (DrawableObjs * drawObjs then it can iterate through them and call the common Draw() function so that each object can draw itself. there are many many reasons to use polymorphism. it''s one of the most powerful tools in the C++ tool chest.

Inheritence again is another powerful tool with which you can inherit common properties of objects. for instance, every object in your world will need a matrix to hold it''s unique position & orientation. why code 30 different classes each with their own matrix member var (Enemy, Monster, Bullet, etc) when you can have a single base class with the matrix var and other common things.

Basically you are asking why is Object Oriented design useful. This question can best be answered by learning what OO design is, and then you should see why it can be useful. It''s obviously not the only way to do things. equally powerful engines have been written with procedural design. it''s a tool that has some very powerful uses. it''s a good thing to learn about.

-me

Share this post


Link to post
Share on other sites
Palidine    1315
quote:
Original post by markr
Polymorphism is just the RIGHT way to do things.



No, just A way to do things. you can write equally powerful and concise apps using either OO design or procedural design. sometimes OO is more useful, sometimes procedural is more useful. they''re both just tools that you can use to solve problems. neither is inherently better than the other.

For instance, AFAIK, no id software engine has so far been written with OO design. I doubt you can claim that their engines are "wrong".

-me

Share this post


Link to post
Share on other sites
jollyjeffers    1570
quote:
no id software engine has so far been written with OO design
just to be picky I thought that Carmack switched the graphics engine to C++ around the Quake-3 build. Whether that was just "C with a C++ compiler" or proper OOP C++ I dunno.

Jack

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by jollyjeffers
quote:
no id software engine has so far been written with OO design
just to be picky I thought that Carmack switched the graphics engine to C++ around the Quake-3 build. Whether that was just "C with a C++ compiler" or proper OOP C++ I dunno.

Jack


To be honest I don''t think there have been many (any ? LCC maybe) widely used pure C compilers - I thought since the late 1990''s the situation has been that C is conventionally compiled on a C++ compiler in a pure C mode, which I assume is what you''re insinuating.

Share this post


Link to post
Share on other sites
jollyjeffers    1570
quote:
which I assume is what you''re insinuating


probably I just remember reading some comments from one of Carmacks .plan files (or just general news) regarding some sort of switch from C to C++ for the Quake/Doom graphics/game code..

Jack

Share this post


Link to post
Share on other sites
Shannon Barber    1681
I don''t think you''re using the term singleton correctly - singleton refers to a particular object-oriented design pattern.

In order for singleton to be at all useful, it must use polymorphism (and consequentially inheritence). Otherwise, it''s just a glorified global variable, which is the typical (ab)use.


...
The graphics engine is still written in C, the new ID game programmers think he''s crazy and finished the game using C++. ID uses a pipelined development process, Carmack does research and develops the next graphics engine, while the normal programmers finish the actual game (and then Carmack begins work on the next project).

Share this post


Link to post
Share on other sites
cilcoder    122
umm, I haven''t looked at the quake/quake2 source in a while but although it is C it does seem OO in some cases. Like the entities. But maybe I''m thinking about some other enigne I looked at.

Share this post


Link to post
Share on other sites