[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programming it's a play

From: Nicolas Roard
Subject: Re: Programming it's a play
Date: Sat, 20 Sep 2003 20:28:33 +0100

On 2003-09-20 17:44:20 +0000 Dennis Leeuw <address@hidden> wrote:

My problem here (and the main reason why invented the actor story) is the fact that (atleast in Objective-C) a class is an object as soon as it is compiled, since you can send it a message of new.


And that is not something I can do with school. If I would send a new message to school I 1) get a new school or 2) would get an actor, 3) school sends out an actor.

1) Is not what I want
2) Has a name change (school gets actor) which is also not what happens in Objective-C 3) This is also, as far as I understand it, not what happens. The class object instantiates an object which is a copy of itself with (possibly) it's instance variables changed. But the methods are always identical. While the methods of a school and an actor are not identical.

Hm, no, but it's more like #3, the class effectively creates an object and 
it; the class acts as an object factory !

The "new" method is a bit strange at first glance, because it mix two
calls semantically different in objective-c -- a call to "alloc" followed by a 
to "init". The problem is that alloc and init are two different beasts, alloc 
a class method and init beeing an instance method. But new is just that, a
shortcut for us lazy programmers ;-) to be able to simply write
MyClass* myObject = [MyClass new]; // instead of
MyClass* myObject = [[MyClass alloc] init];

The second line is in fact way clearer in how it works :
MyClass* myObject;
myObject = [MyClass alloc]; // MyClass returns an object
[myObject init]; // we initialize the instance

Two types of methods exists as you know : class methods (+) and instances
methods (-). Class methods only works on the class itself, instances methods
only works on an instance of a class (ie, an object). imho speaking of "class 
is a bit misleading -- as the definition of an object is to be an instance of a 
you could send messages directly to the class, but it's not an object per se...

When you create an object, you need to ask the class itself, using basically
the alloc/init scheme :

"alloc" is a class method -- only callable on the class itself, not on an 
of the class. Its job is to simply create an object in memory, that is, reserve
enough memory space for the object and its attributes, and returns a pointer
on the created object (~ the allocated memory space if you want).

"init" is an instance method and as it, only works on instances, not on the 
Its job is to initialize the previously allocated memory space (initialize the
attributes). It's quite logical in fact, as the instance now "where to work" in

The class could effectively be considered as a factory of objects, because
the job of the "alloc" method is to create objects. After that, you need to
initialize the object, and you use the instance method init. The class
creates an instance, and you could send to this instance the messages
listed by the class; that is, the class define how an instance respond
to a message (ie a method call)

My initial remark was that with your actor metaphor, you don't highlight
much the fact that you could have different instances of the same class,
running at the same time, and possibly cooperating (as you assimile
an object to a role and generally, you don't have a same person (your
class) performing different roles *on the same time*). Furthermore, in OOP
the general analogy is to consider objects as living entities; that's why
imho it's better to assimile an object to "an actor" (a person) rather than
a role, and a class to an "actor's factory"

Please correct me if I am wrong since it is only since today that I feel I realy understand the difference between a Class, a Class object, an Object and an Instance.

Well, imho you shouldn't speak about a "class object" because it's a bit
misleading -- it's not really an object. And an object IS an instance, it's the
definition. It's an instance of a class of objects.

Which to me, and I have noticed to more beginners, is a very confusing concept. So enlightenment is very much welcomed.

Yes, and that's not all that easy, because the OO concepts used in ObjC
differs a bit from C++; but ObjC functionment is largely clearer.

And it might be that I am wrong on the feeling part :)

Thanks for the input,


Nicolas Roard <address@hidden>
PGP : http://www.roard.com/download/key.gpg.asc
Work for something because it is good, not just because
it stands a chance to succeed.  - Vaclav Havel

reply via email to

[Prev in Thread] Current Thread [Next in Thread]