Difference between static function and singleton class in swift [closed]
I want to create a class where all utility methods will be kept and these methods will be used throughout the app.
Problem:1
Is it good to create a singleton class and keep all necessary methods there or should I create a class where all function will be static.
Problem:2
What is main difference between above two approaches in swift ?
Problem:3
How it will impact performance and memory in iOS?
Sure this sounds confusing and can be debated. However, from the best practices i can put some suggestions.
Singleton is usually used to create a resource intensive and one timer initialisation for instance: a database connector, login handler and such.
Utility class are classes that only have static functions and variables. It should not deal with async task and expensive resource handling like opening a database connector.
In your case, if the utility is doing some resource intensive process its better to wrap in a singleton. If not, then Static functions in a class is better in my opinion. This is so also because, Swift will dispatch all static functions in a class using static dispatch. Whereas this cannot be true in Singleton although Swift likes to optimizes.
Static Dispatch are 4 times faster than Dynamic Dispatch as far as Objective-C runtime is used. This is true for swift too. However, it only takes 4 nano seconds to dispatch Dynamiclly.
I hope this makes you clear.
Lets say we have two classes.
Class with all static methods and static variables This way you can access the class methods without instantiating an object. Also if some small data handles are required they can be stored in static variables. All the threads accessing this class would not end up creating duplicate instances of data variables.
Singleton Class This class will have a private init method and share a single instance through a static instance. All the threads accessing the instance would not end up creating duplicate instances of data variables.
So technically both may sound very similar for your scenario of a Utility class and it might get confusing to make a decision, you may use following use cases to make a decision.
Does this class primarily defines logical implementations? If the primary purpose of class methods is to provide logical calculations/manipulations or operations that do not require storing data in variables besides temporary handles then you should always opt for a Static Class. For e.g. Utility class that you have mentioned here. Utility methods like resizing an image, reading a file, parsing a data structures are are best done using Static methods.
If your class requires storing important information in multiple variables, limiting resource access (e.g. limiting number of simultaneous transactions on database or limiting number of simultaneous network calls), where it frequently allocates and deallocates resources (manages the memory at runtime) then its best to use a Singleton class (For e.g. class for accessing db structures, managing network resources, etc..)
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.
So if you want to create class for utility methods it should be a class with static utility functions and not singleton. Both static and singleton class can be implemented thread safe.