Jump to content

Objects

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Nightracer

Nightracer

    Programmer

  • Members
  • PipPipPipPip
  • 131 posts

Quote

an object is an individual unit which is used as the basic building block of programs.
These objects act on each other, as opposed to a traditional view in which a program may be seen as a collection of functions, or simply as a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects.
Each object can be viewed as an independent little machine or actor with a distinct role or responsibility.

So I think we all know what objects are.
But how do you determine what to make an object? I often make the mistake of having TOO many objects.
How do you decide what should be an object?

#2
dirkfirst

dirkfirst

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 354 posts
Good question......... I don't think there is a correct way to determine what to make an object. I would make anything an object that you plan on using more than once such as a wrapper.

Objects are classes, correct?

#3
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Quote

Objects are classes, correct?

Right.

Lol to many objects? Ive made entire programs all in class's, ie the entire content of main on a mac space invaders game i made ( for fun ) was:


void main( void )

{

WORLD world;

WindowPtr win;

int x=0,y=30;


world.init( &win );

for( int x=0;x<20;x++)

world.en_new( world.enemy , x+10 , y );

world.sp_spawn();


do

{

world.setworld( world.offscrn );

world.en_move();

world.en_update();

world.sp_checkkey();

world.setworld( world.window );

world.sp_draw();

world.en_draw();

world.speed();

}while(1);


}



#4
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

Nightracer said:

So I think we all know what objects are.
But how do you determine what to make an object? I often make the mistake of having TOO many objects.
How do you decide what should be an object?

There's a lot of different methods to designing a system: CRC cards, UML diagrams, user stories, etc. For the most part, your classes (I use "class" when talking about code, "object" when talking about a program - a class is a definition of an object) will be modeled after reality - whatever reality your system has to deal with.

A simple method to get you started is to describe your system, and then pick out the nouns you used to describe your problem domain. Those nouns are usually good candidates for a class.

Though I'd say it's difficult, it's not impossible to have "too many classes". But really, it's more likely that you (like I have a tendency to do) are complicating the subject matter. In your complex modeling of the reality, you've identified a lot more players than are actually needed to perform the job. So, your problem isn't really that you have too many classes, it's that your model is too complex.

In general (excepting the case above - which is a modeling problem), the more classes the better. By having more classes, each class can have a well defined role (the Single Responsiblity Principle) and can be understood easily. By taking for granted that a class does its responsibility correctly, it's easier to understand a small part of the system without having to grasp all of the the code at once.

#5
Nightracer

Nightracer

    Programmer

  • Members
  • PipPipPipPip
  • 131 posts

dirkfirst said:

Good question......... I don't think there is a correct way to determine what to make an object. I would make anything an object that you plan on using more than once such as a wrapper.

Objects are classes, correct?

No, an object is an instance of a class..

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'll give you an example: Let's say you want to make a chess game. Classes that come to mind for me are:
Game
Board
Piece (inherit down to individual types)
Turn
Player
History

Objects of these classes will interact for things such as attacks, pieces knowing how they move and where they are, checking for stalemate by repetition, etc. Think about what objects exist in the problem, and you get your classes.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
One classic way to define your classes would be to write out your different scenarios, and then look at the nouns. Those are good candidates for classes. The verbs are good candidates for methods.

#8
Nightracer

Nightracer

    Programmer

  • Members
  • PipPipPipPip
  • 131 posts
Wingedpanther again you post a great response!
Love the examples you give, it really clears things up as far as explaining goes!

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You should see me when I teach math!
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog