Friday, August 05, 2005

Actors

New class for actors was added to StepTalk. What are actors in this context? They are objects with own attributes and methods written in a script. One can use actors for example for:
  • fast prototyping of objects
  • user-customisable objects or assistants
  • application extensions

Each actor has to be "put into an environment" - programmatically it means that one has to assign an environment to the actor.

Here is a short example written in Smalltalk, easily rewritable to Objective-C. First we create an actor:

actor := STActor actorInEnvironment:Environment.

Then we create attributes (instance variables):

ivars := NSMutableDictionary dictionary.
ivars setObject:1 forKey:'number'.
actor setInstanceVariables:ivars.

Now, to create a behaviour, one has convert scripts into a methods. This conversion is done using an engine. Therefore we need to get the proper engine for a language of our choice, which is Smalltalk at this time:

engine := STEngine engineForLanguageWithName:'Smalltalk'.

Create a new method script:

source := 'increment number := number + 1. ^self'.

Create method and add it to the actor

method := engine methodFromSource:source forReceiver:actor inEnvironment:Environment.
actor addMethod:method.

Add few other methods in a similary way: setNumber: - set value of i; print - print i on Transcript.

Finally send the messages and see it work:

actor print.
actor increment.
actor print.
actor setNumber:10.
actor print.

Script for this example can be found in StepTalk sources Examples/Smalltalk/actor.st

0 Comments:

Post a Comment

<< Home