Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Struct based and Class based singletons?

Tags:

swift

Are the two approaches the same or are there major differences/pitfalls to be aware of:

class MyClassSingleton {   static let sharedInstance = MyClassSingleton()   private init(){}    func helloClass() { print("hello from class Singleton") } }  struct MyStructSingleton {   static let sharedInstance = MyStructSingleton()   private init() {}    func helloStruct() { print("hello from struct Singleton") } } 
like image 307
Running Turtle Avatar asked Apr 22 '16 07:04

Running Turtle


People also ask

Can a struct be a Singleton Swift?

Only classes can be singletons in Swift. Structures are value types, and copies are created every time you pass a value to a function or a type. This clearly defeats the purpose of singletons.

What is the difference between Singleton and static class Swift?

Major difference between static and singleton is that Singleton can implemented Protocols and derive from some base classes. In case of Singleton , class can be instantiated but only once. Static functions can be used directly without instantiation.

Should I use 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 is Singleton instance static?

The purpose of Singleton is to have only one instance of that object[1]. By making a sealed class with a private static instance which is automatically instantiated on first access, you provide its necessary trait of only creating one copy of the actual object that is then accessed by the Singleton.

What is the difference between static and singletons?

Static classes are basically a way of grouping classes together in Java. Memory is allocated once the object is created. Memory is allocated immediately after any of the class members is accessed. Singleton implementation can either have static members or instance members. Static classes can contain static members only.

What are the advantages and disadvantages of using a singleton class?

Your code will be more flexible if you are using a singleton class and another advantage is that the code that uses the singleton does not need to know if it is a singleton object or a transient object. Using a static means you have to call static methods explicitly. Static classes also fail during dependency injection.

What is singleton implementation in Java?

Singleton implementation can either have static members or instance members. Static classes can contain static members only. It can implement any other interface or base class is required. It cannot implement the interface or any other base class. Singleton classes can be used as a method parameter.

What is the difference between a struct and a class?

A Structure is not secure and cannot hide its implementation details from the end user while a class is secure and can hide its programming and designing details. Following are the points that expound on this difference: 1) Members of a class are private by default and members of a struct are public by default.


2 Answers

The main difference is that class-based mutable singleton works, while struct-based mutable "singleton" doesn't. Unless you want to make your singleton immutable (which is rare) you should stick to the class-based one.

Here is an illustration of how mutable struct-based "singleton" does not work. Consider adding a mutable member state to both singletons, like this:

class MyClassSingleton {     static let sharedInstance = MyClassSingleton()     private init(){}     var state = 5     func helloClass() { print("hello from class Singleton: \(state)") } }  struct MyStructSingleton {     static let sharedInstance = MyStructSingleton()     private init() {}     var state = 5     func helloStruct() { print("hello from struct Singleton: \(state)") } } 

I made state a var, but I could expose it as a read-only property plus a mutating method; the essential thing is that both types are now mutable.

If I do this

let csi = MyClassSingleton.sharedInstance csi.state = 42 MyClassSingleton.sharedInstance.helloClass() 

42 gets printed, because csi is referencing the shared instance.

However, when I do the same thing with struct-based singleton

var ssi = MyStructSingleton.sharedInstance ssi.state = 42 MyStructSingleton.sharedInstance.helloStruct() 

5 gets printed instead, because ssi is a copy of the sharedInstance, which is, of course, an indication that our singleton is not actually a singleton.

like image 146
Sergey Kalinichenko Avatar answered Sep 29 '22 03:09

Sergey Kalinichenko


That depends on what you want to achieve and how you want to use your structure based on differences between class and struct. Most common thing that you will see is using class with singleton object.

Singletons are pretty much the same, they are only created once, but you will get different behaviors from the class and from struct because:

  • Classes are reference types, while structs are value types
  • Structs are used to define simple structures
  • Structs can't be inherited

There are several more differences but you get the idea from this.

like image 34
Said Sikira Avatar answered Sep 29 '22 03:09

Said Sikira