Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton Class in Flex

I have a doubt,.... How would you create a Singleton class in Flex...

Is there any convention like the class name should eb Singleton or it should extend any other class.

How many Singleton class can a project have?

Can anyone say the real time usage of a Singleton class?

I am planning to keep my components label texts in a Singleton class... Is it a good approach.

like image 464
Kevin Avatar asked Aug 21 '09 14:08

Kevin


People also ask

When to use Singleton design Pattern?

Singleton pattern is used for logging, drivers objects, caching and thread pool. Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade etc. Singleton design pattern is used in core java classes also, for example java. lang.

Why Singleton?

A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.

What is a Singleton in programming?

A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class to only one object.

What is singleton class in c3?

Singleton Class allow for single allocations and instances of data. It has normal methods and you can call it using an instance. To prevent multiple instances of the class, the private constructor is used.


1 Answers

Can of worms asking about singletons!

There are a few different options about creating singletons mainly due to AS3 not having private constructors. Here's the pattern we use.

package com.foo.bar {

    public class Blah {

        private static var instance : Blah;

        public function Blah( enforcer : SingletonEnforcer ) {}

        public static function getInstance() : Blah {
            if (!instance) {
                instance = new Blah( new SingletonEnforcer() );
            }
            return instance;
        }

        ...
    }
}
class SingletonEnforcer{}

Note that the SingletonEnforcer class is internal so can only be used by the Blah class (effectively). No-one can directly instantiate the class, they have to go through the getInstance() function.

like image 53
Gregor Kiddie Avatar answered Sep 19 '22 03:09

Gregor Kiddie