Why Not Just Use Singleton Classes?

Started by
10 comments, last by dave 20 years ago
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
Advertisement
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.
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
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
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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
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

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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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.
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

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement