Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: Static class versus Singleton

I know SO pals are not fans of "versus" questions but... even rephrasing the tittle a bit, the versus part is still there, so, why hide it.

Basically, I'd like to know when and why should I use a singleton or a static class, what can offer a singleton that a static class cant, and vice versa.

For a long time I used both, and I cant see a reason why I shouldn't use one over the other.

Thanks.

like image 879
Artemix Avatar asked Dec 08 '12 23:12

Artemix


People also ask

Which is better singleton or static class?

A Singleton can implement interfaces, inherit from other classes and allow inheritance. While a static class cannot inherit their instance members. So Singleton is more flexible than static classes and can maintain state. A Singleton can be initialized lazily or asynchronously and loaded automatically by the .

Why can't we use static class instead of singleton?

While a static class is generally initialized when it is first loaded and it will lead to potential class loader issues. Singleton Objects stored on heap while static class stored in stack. Singleton Objects can have constructor while Static Class cannot.

Why we use singleton over static class C#?

The Singleton class does not require you to use the static keyword everywhere. Static class objects cannot be passed as parameters to other methods whereas we can pass instances of a singleton as a parameter to another method.

What is the difference between singleton design pattern and static class in C #?

In C#, it is possible to implement interfaces, inherit from other classes and allow inheritance with the Singleton class. These are not possible with a static class. So the Singleton class is more flexible as compared to static classes.


1 Answers

Both are basically global variables with caveats. Static classes prevent inheritance, and Singletons are ugly and add overhead with a property lookup. Both make automated testing difficult.

AS3 supports global variables, so why not use those?

Static:

package com.example
{
    public class Config
    {
        public static var VERSION:uint = 1;
    }
}

Singletons:

package com.example
{
    public class Config
    {
        public static var instance:Config = new Config();

        public var version:uint = 1;

        public function Config()
        {
            //Boiler plate preventing multiple instances
        }
    }
}

Global variable:

package com.example
{
    public var config:Config = new Config();
}
class Config
{
    public var version:uint = 1;
}

Now, let's say even though you only want a single instance of the class in your production app, you need multiple instances to write tests. You can make a public class Config, and use com.example.config = new Config() to reset. All the places that reference your global variable are now using the new instance, and you can even do fancy things like inheritence.

For example:

if(inDebugMode)
{
    com.example.config = new DebugConfig();
}
{
    com.example.config = new Config();
}
like image 163
Sean Fujiwara Avatar answered Nov 02 '22 01:11

Sean Fujiwara