Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton VS static(class) variables [closed]

Tags:

macos

ios

swift

What is the best practice in Swift?

Option 1:

class SomeManager {

    static var sharedManager = SomeManager()

    var someVariable: String?

}

and then

let something = SomeManager.sharedManager().someVariable

Option 2:

class SomeManager {

    static var someVariable: String?

}

and then

let something = SomeManager.someVariable
like image 222
Artem Deviatov Avatar asked Mar 30 '16 14:03

Artem Deviatov


People also ask

Can a singleton class have static variables?

Definitely the most common usage is that singleton is normal object that holds member variables. You can, for example, easily replace one object with another (with all other properties). Every class can have static variables but it does not deppend wheter it is singleton or not.

What is the difference between singleton class and 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 .

Is singleton class sealed?

Marking the class sealed prevents someone from trivially working around your carefully-constructed singleton class because it keeps someone from inheriting from the class.

Why is singleton better than static?

Extensibility. It is not possible to inherit from a static class, while it is possible with singleton pattern if you want to allow it. So, anyone can inherit from a singleton class, override a method and replace the service.


1 Answers

tl;dr

Option 1 (class or struct) when you store mutable state because you need other instances.

Option 2 (scoped global variables) when you want to store static variables because it's faster and uses less memory.

Singleton Class (or struct) with variables

Global state is generally considered a "bad thing". It's hard to think about, causes problems but is sometimes unavoidable.

  • Create a class if you ever want to have multiple SomeManager instances.
  • A singleton can be good default instance but there may be edge cases where you want to have separate behavior (testing).
  • Dependency Injection... is big topic that is relevant if SomeManager is storing global state.

Static Variable

  • Always use when the someVariable is a constant.
  • Does not require extra storage for static var sharedManager = SomeManager(); you use only the memory which you actually need.
  • Slightly faster because you do not need to load sharedManager into memory then access it's member someVariable. You straight up access someVariable.

Bonus Tip:

In Option 2 you can create SomeManager even though it doesn't do anything. You can prevent this by turning SomeManager into an enum with no cases.

enum SomeManager {
    static var someVariable: String?
}

You can still do this:

SomeManager.someVariable

but you can't do this

let manager = SomeManger()
like image 92
Kevin Avatar answered Sep 18 '22 17:09

Kevin