The way in which objects are created depends on a computer's compiler and the software libraries available to the user. The implementation of Objective C on a system using the GNU compiler will not be exactly the same as the implementation on a Next system. While most of the points made in the literature on Objective C easily carry over to Swarm modeling, the commands needed to create objects are an exception. In the Objective C manual for Next systems, for example, one finds a syntax methods init and alloc that are not used in Swarm. That's why a brief study of object creation is important.
Pick any Swarm application you like, such as Heatbugs. Look in main.m. What do you find? There's a check to see if the GUI mode or batch mode is to be run, and depending on that choice, either the ObserverSwarm or the BatchSwarm is designated as theTopLevelSwarm.
Suppose we have do not do anything special when compiling and running the heatbugs executable, so the GUI mode is used. In that case, the relevant code in main.m is this:
if (swarmGUIMode == 1) { theTopLevelSwarm = [HeatbugObserverSwarm createBegin: globalZone]; SET_WINDOW_GEOMETRY_RECORD_NAME (theTopLevelSwarm); theTopLevelSwarm = [theTopLevelSwarm createEnd]; } |
The first command inside the brackets tells the class HeatbugObserverSwarm to execute its createBegin method and return an object which is to be named theTopLevelSwarm. In this example, the HeatbugObserverSwarm is the class and also serves as a "factory object", an object that can build instances of its class. The second command is a macro that saves window positions on subsequent runs of the program. It is set between the createBegin and createEnd methods because it is setting permanent features of the object theTopLevelSwarm. The last command "seals" off the creation phase by telling the recently created object theTopLevelSwarm to run its createEnd method.
In the Swarm Reference guide, many of the protocols have methods that are divided between three phases. The phases are "Creating", "Setting", and "Using". It is important to pay attention to the phase in which a method is listed. Methods or macros listed in the Creating phase must only be used between the createBegin and createEnd messages. If such a method is used after the createEnd, it will cause the program to fail. Similarly, a method in the Using phase must be used only after the createEnd method has finished. Methods in the Setting phase can be used at any time in an object's life cycle.