Thursday, 1 January 2009

Our first Objective-C program

Now we finally gone start.
First fire up XCode and create a new project, for the moment we don't gone use Cocoa , so you do the following : XCode>File>New Project and from the project wizard you select 'Command Line Utility;Foundation tool".
Give your project a name and we can start.

We will implement the classes I discussed before (Human, Male, Female) ; see previous blogmessages for this.

Now first of all in Objective-C a class has two parts , in the .h file you have the class definition (or interface) in the .m file you have the class implementation (or implementation).

(to create a class , select in XCode>File>New File , select Objective-C class and give it a name).

The first class we gone create is the Human class :
(sorry for the layout of the code, you can always sent me an email then I drop you the complete project via mail)

First we define the interface of the class (this is the Human.h file) :

#import
@interface Human : NSObject
{
NSString *name ; }
+ (id) new: (NSString *) s ;
+ (id) new ; + (void) privateNew ;
- (void) name: (NSString *) s ;
- (NSString *) name ;
- (BOOL) isFemale ;
- (BOOL) isMale ;
@end


And then we define the implementation of the Human class (.m file)

#import "Human.h"
@implementation Human
+ (id) new: (NSString *) s
{

[self privateNew] ;
return nil ;
}
+ (id) new
{
[self privateNew ] ;
return nil ;
}
+ (void) privateNew
{
NSException *myExp ;
myExp = [ NSException exceptionWithName:@"AbstractClass" reason:@"Abstract class cannot be instantiated" userInfo:nil ] ;
[myExp raise] ;
}
- (void) name: (NSString *) s
{

name = s ;
}

- (NSString *) name ;
{ return name ; }

- (BOOL) isFemale
{ [self subClassResponsibility] ; }

- (BOOL) isMale
{ [self subClassResponsibility] ; }

- (void) subClassResponsibility
{
NSException *myExp ;
myExp = [ NSException exceptionWithName:@"SubclassResponsibility" reason:@"should be implemented in subclass" userInfo:nil ] ;
[myExp raise] ;
}
@end


Already a few comments on this class :

First of all this is an abstract class, this means that is not ment to have instances. In Objective-C their is no language construct that indicates that a class is abstract.
Thats why I overload the 'new' message. I somebody sent's a new message to the class Human , I'll raise an exception.

Another element are the isFemale and isMale methods (or messages). These methods are supposed to be implemented by the subclasses of Human.

So my 'good' practice is to raise an exception (subclassresponsibility) whenever a subclass does not implement a method.

For those with a Smalltalk background , yes I know , I 'stole' that good practice from Smalltalk. I find this very handy especially when I develop frameworks which must be used by other people; then at runtime at least they known that they forgot to implement something.

I still need to add the code for the 2 other classes Female and Male. I will quickly share the code now and give my comments tomorrow :

The interface for Female (pretty empty no ?)

#include "Human.h"

@interface Female : Human {

}

@end


and the implementation of it

#import "Female.h"


@implementation Female

+ (id) new
{
return [[ self alloc] init ] ;
}

+ (id) new: (NSString *) s
{
Female *anInstance ;

anInstance = [[ self alloc] init ] ;
[anInstance name: s] ;
return anInstance ;
}

- (BOOL) isMale
{
return NO ;
}

- (BOOL) isFemale
{
return YES ;
}
@end


And the interface for Male (also pretty empty)

#import "Human.h"

@interface Male : Human {

}

@end


And the corresponding implementation

#import "Male.h"
@implementation Male

+ (id) new
{
return [[self alloc] init ] ;
}
+ (id) new: (NSString *) s
{
Male *anInstance ;

anInstance = [[ self alloc] init ] ;
[anInstance name: s] ;
return anInstance ;
}

- (BOOL) isMale
{
return YES ;
}

- (BOOL) isFemale
{
return NO ;
}

@end




As you can see , both the Female and Male classes are pretty simple. They both implement the isMale and isFemale method in a different way.
Now with these 3 classes we have more or less implemented the core concepts of OO (more concepts will follow later on).
You have a super class (Human) which understands 4 messages (name, name:, isFemale, isMale). You have 2 subclasses (Male, Female) which has a different implementation for the messages isFemale and isMale.
I also introduced 2 types of messages (lets call them methods now) : class methods and instance methods.

In a next blogmessage I'll explain a bit more on the 'syntax sugar' of this Objective-C implementation

No comments:

Post a Comment