Menu

Creating objects on the stack

Mark Anthony Taylor (Shyreman)

Given an interface, a class definition that implements that interface, the set of methods that are implemented for that class, and a factory for creating objects of that class, one can create class instances (objects) on the stack:

(<interface-name> <instance-name> (factory-name <factory arg1><factory arg2>))

So given Sys.CreateDog and Sys.IDog as defined in the previous pages, we can create a dog on the stack thus:

(IDog dog (CreateDog 10 6)) // constructs Dog with age 10 and breed 6.

This is called a factory call.

If we remove the factory invocation, we create an instance of the null-object associated with
the dog interface.

(IDog dog) // creates a null-dog.

The size of a null-object is just enough to contain the largest class instance that implements
the interface associated with the null-object, allowing us to defer creation to a factory call
to a later time. In this case we can use a deferred factory call.

(dog = (CreateDog 10 6)) // constructs a Dog with age 10 and breed 6 over the null-dog instance.


Related

Wiki: Content

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.