Singleton

From Director Online Wiki
Jump to: navigation, search

A singleton is an object that can get the creator call several times, but still exists only once. This makes sense for objects that should provide the same member properties or methods for different parts of the movie.

Why not call an object only once in the first place? Well, you could do this, but this can be hard when several independent scripts need the singleton. When they are really independent, it's often impossible to know whether the other script already created that object.

Would that object be a singleton-script, then you could easily create the object from every script that tries to use it. The singleton itself will check whether its already existant and return the first instance if necessary.

Where are singletons useful=

on manager scripts

Let's take an actorList-replacement as an example. Such a manager should exist only once - so there will only be one new actorList.

on memory extensive objects

Imagine a script that creates an big image, that you want to use in several parts of the movie. Creating the image-script several times would consume more memory than useful.