Object/classes visualisation


#1

Hi Guys

I find I learn things better when I can clearly visualise them. To help myself understand Objects, I have created a diagram, but I’m not sure how accurate it is. Can an expert have a look at tell me if I’m close?

Thanks


#2

That looks nice. Are you familiar with UML Object and Sequence Diagrams?

en.wikipedia.org/wiki/Object_diagram
en.wikipedia.org/wiki/Unified_Modeling_Language


#3

No Brian, that’s the first time I’ve heard of UML. I will check it out.

It really helps me cement ideas in my memory when I draw them.


#4

Im am also like that. Its called being visual spatial, you learn better if you can visualize things.
I just finished the book, and one of my favorite analogies for the object-class-framework-method-variable relationship is as follows:
(sorry i dont have time to make a diagram, and btw your diagram is very correct too)

Pretend that a framework is a car retailer (ford or chevy, for example). It carries many different types of cars (for example chevy carries the Malibu, the Equinox, the silverado, etc). The types of cars are analogous to classes. Each car type has certain accesories that are available (such as bluetooth, sirius XM, a dvd player). These are analogous to class variables. Then, you could have a specific model of one of those car types (for example, you might own a Malibu), which has its certain accessories (sirius xm and a dvd player, but not bluetooth). That instance of the car type is an object, with the accessories being the instance variables.
Then methods would be analogous to when you tell your Malibu to do something (such as turn up the radio or turn right).

in code, that same thing might be as follows:

the framework, which holds all of the classes:
#include <Chevy.h>

then you would have classes for the different models (here is just the class definition for the Malibu):
malibu.h:

[code]@interface Malibu : NSObject
{
BOOL hasBluetooth;
BOOL hasSiriusXM;
BOOL hasDVDPlayer;
NSString *carName;
}

  • (void)turnRight;
  • (void)turnLeft;
  • (void)goForward;
  • (void)brake;

@end[/code]
malibu.m:

[code]#import “Malibu.h”

@implementation Malibu

  • (void)turnRight
    {
    //code that makes it turns right
    }

  • (void)turnLeft
    {
    //code that makes it turns left
    }

  • (void)goForward
    {
    //code that makes it go forward
    }

  • (void)brake
    {
    //code that makes it brake
    }

@end[/code]

then, analogous to buying your malibu, you make an instance of malibu in main.m, specified its instance variables, and practiced its methods:

[code]#import <Foundation/Foundation.h>
#import “Malibu.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    Malibu *myMalibu = [[Malibu alloc] initWithCarName:@"my Malibu" hasDvdplayer:YES hasBluetooth:NO hasSiriusXM:YES];
    [myMalibu turnLeft];
    [myMalibu turnRight];
    [myMalibu goForward];
    [myMalibu brake];
    
}
return 0;

}
[/code]
(i realize that those arent valid initializers, i didnt have enough time to set all those)

It took me awhile to grasp the concept of objects, so don’t be discouraged. I hope this helped :slight_smile: