3.4. Giving Life to Classes: Instantiation

After the code is written to implement the class (with .h and .m files for Objective C and .java in the Java case), there is still work to be done. Instances of the class must be created. The creation of instances of a class is one of the specialized features of Swarm. Since the instantiation process can be sometimes different from the that described in the Objective C and Java literature, it is worth some special attention.

The creation of the substantively important objects is often handled in the model swarm. This process uses the specialized memory management and object creation code in the Swarm library.

3.4.1. Instantiation: Objective C Style

The objects that represent the actors in a simulation--the substantively important entities--are usually subclassed from the SwarmObject class. The "inheritance hierarchy" that leads to the class SwarmObject passes through classes that allow the creation and deletion of objects from a simulation. Objects are often created by a pair of "bookend" commands, createBegin and createEnd. This is not part of the Objective C syntax. Rather, it is unique to Swarm.

Suppose the Bug.h and Bug.m files from previous exist, and one wants to create an instance of that class. In a file ModelSwarm.m, one would typically have a method called buildObjects, which is usually a method that houses all object creation. For example:

// Excerpt from ModelSwarm.m that creates a Bug instance
#import "Bug.h"
// {other imports and code that defines schedules, etc}
- buildObjects 
{    
    id  aBug;
    bug = [Bug createBegin: self];
    // commands that set permanent features of the agent can apppear here
    bug = [Bug createEnd];
}

The class's "factory object", Bug, is told to create an object in a memory zone that is provided by ModelSwarm (ModelSwarm is the self.). Then the object aBug is instructed to finish the creation process, after optional commands are added to define the features of the object (typically, to set permanent structural aspects of the class). Many of these subtleties are explained in depth in later sections (see also Appendix B).

Object instances need not be created by the createBegin/createEnd pair. Objects can often be created by a simple create command [1].

aBug = [Bug create: self];

In code written for older versions of Swarm, one will often see a slightly different syntax in ModelSwarm.m:

aBug = [Bug create: [self getZone]];

In Objective C, this usage is still valid, although it is deprecated. Since now the objects of type Swarm, like the model swarm itself, are memory zones, there is no need to get a zone in which to put the bug. Rather, the bug can be put in the zone that is provided by the model swarm itself.

3.4.2. Instantiation: Java Style

For most stock objects created in application Java code (including user-created Java classes), the entire createBegin/createEnd apparatus can be dispensed with. Java uses the term constructor for the method that creates an instance of a class. The Java interface to the Swarm libraries have "convenience" constructors which essentially bracket the entire set of create-time messages in a single call to the constructor (or call the constructor with no arguments as in the present case). The simplest method to create a Java Bug object is to invoke the following:

aBug = Bug (this.getZone());

This is equivalent to the final Objective C example given in the last section. In summary here is the comparison:

Objective C exampleJava example
aBug = [Bug create: [self getZone]];
aBug = Bug (this.getZone());

Note that the explicit create: method in the Objective C case, is made implicit in the Java case.

Caution

It is still possible to use the create and createBegin/createEnd apparatus in Java, but due to Java's strongly-typed nature, it can require considerably more coding overhead than in Objective C, and will be left to later version of the Guide.

Notes

[1]

For example, the Swarm collections library includes a class called List, which is most often created this way.