GODPAPER | All Packages | All Classes | Index | Frames No Frames |
SingletonFactory

Package | com.godpaper.as3.utils
Class | public class SingletonFactory
Inheritance | SingletonFactory Object

One of the problems with the Singleton design pattern (there are many) is that it encapsulates the single instance nature of the pattern within the class, when very often the requirement for only one instance is not a feature of the class but a feature of the application that is using the class. So I created this factory function to create and manage a single instance of any class. There is no need to modify the class itself, just ask the function for an instance of the class and it will always return the same instance. var a:SomeClass = SingletonFactory.produce( SomeClass ); You can still create additional instances of the class using the new operator, but if you use the singletonFactory function to get an instance it will always return the same instance.

Public Methods

| Method | Defined By
--- | --- | ---
|  | produce(type:Class):* [static] Used to create a singleton from a class without adapting the class itself. | SingletonFactory

Method Detail

produce | () | method

`public static function produce(type:Class):*`

Used to create a singleton from a class without adapting the class itself. This function returns the same instance of the class every time it is called. This function only works with classes that don't require parameters in the constructor.

Parameters

| `type:Class` — The class you want a singleton from

Returns

| `*` — the singleton instance of the class

See also

http://code.google.com/p/bigroom/source/browse/trunk/src/uk/co/bigroom/utils/singletonFactory.as

Thu Jan 16 2014, 01:11 PM +08:00