Difference between Singleton class and singleton method? - singleton

I have one question.
What is the difference between singleton class and singleton method. I have created shared instance method, if I use alloc method, it will create new instance or not?
Thanks in advance

The "singleton pattern" (or a "singleton class" or just "singleton") is simply any class for which you are only allowed a single instance.
A "singleton method" would refer to the method that one writes to access the instance. (Nowadays, in Swift and Objective-C, the public interface would declare a property to access the singleton, not a method, but the idea is the same.)
Conventionally the "singleton pattern" refers to a class that simply would not permit the creation of additional instances. But it should be noted that the term "singleton" is sometimes used more loosely in those cases where there exists a single shared instance, but for which you may actually permit creation of your own instances, too. An example of that would be NSURLSession, whose sharedSession property is referred to as a "singleton" in the Apple documentation, but one is still permitted to create ones' own custom NSURLSession sessions.
Bottom line, whether you design your singleton in such a manner that you can or cannot instantiate additional instances is a question of the design of your class. (Many assiduously insist that this is the very meaning of "singleton". I'm not going to participate in that debate.) In many cases, we explicitly want to want to prevent the accidental creation instances. In other (more rare) cases, though, you may want to permit both a singleton as well as the ability to create additional instances. It's just a question of the intent of the singleton's class.
Note, your question was originally tagged objective-c. An Objective-C implementation might look as follows:
NS_ASSUME_NONNULL_BEGIN
#interface Foo : NSObject
#property (class, strong, readonly) Foo *sharedFoo;
- (instancetype)init __attribute__((unavailable("Use +[Foo sharedFoo] instead")));
+ (instancetype)new __attribute__((unavailable("Use +[Foo sharedFoo] instead")));
#end
NS_ASSUME_NONNULL_END
And
#implementation Foo
+ (Foo *)sharedFoo {
static Foo *sharedFoo = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedFoo = [[self alloc] init];
});
return sharedFoo;
}
- (id)init {
if ((self = [super init])) {
// do additional initialization here
}
return self;
}
#end
Note:
Nowadays one would generally define the singleton to be accessed through a #property in the public interface, not as a method. This property would be declared as class and readonly, and you would then manually implement the getter accessor method for that property.
By defining this public interface to be a property rather than a method, should you ever interface with this object via Swift, you will be accessing it using patterns more common in Swift. You don't have to define it as a property, but you generally would.
Note that I defined this property to be sharedFoo, rather than sharedInstance or sharedManager. By calling it sharedFoo, when you bridge this code with Swift, it will automatically be exposed as shared, the more concise name that Swift, as a matter of convention, uses for its singletons and other shared instances.
In this case I have declared init and new as unavailable. This will prevent developers from accidentally instantiating additional instances. Again, this is a question of the purpose of the singleton.
If you look at singletons (or other shared instances) throughout the Cocoa API, you'll see that Apple has transitioned to this property approach.

Related

Should I use +load to generate a singleton?

I'm writing a singleton class, and I usually do this:
#interface MySingleton
+ (instancetype) instance;
#end
#implementation MySingleton
+ (instancetype) instance {
static MySingleton *instance;
if (!instance) {
instance = [MySingleton new];
}
return instance;
}
#end
But I recently considered this pattern:
#interface MySingleton
+ (instancetype) instance;
#end
#implementation MySingleton
static MySingleton *instance;
+ (void) load {
instance = [MySingleton new];
}
+ (instancetype) instance {
return instance;
}
#end
I find this more elegant and easier to understand, but this will be my first time using +load. Are there any pitfalls that I might not expect? Is there any reason this would be a bad idea?
+load has been deprecated/removed for Swift code. While existing ObjC code will still execute it (even if called from Swift), new code should not rely on it. The dispatch_once approach that #gnasher729 references is strongly preferred, and has the benefit of being lazy (while +load slows down launch; dispatch_once doesn't force construction until first usage). The dispatch_once Singleton is even a built-in code-snippet in Xcode. Apple provides the recommended implementation in Adopting Cocoa Design Patterns.
+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
(Or in Swift)
class Singleton {
static let sharedInstance = Singleton()
}
The naming sharedInstance (or better, sharedFoo for the class Foo) is preferred over instance. See +[NSURLSesssion sharedSession], etc. This captures the real nature of singleton in ObjC, which is not really Singleton. There is just a single well-known instance. You are usually free to instantiate additional instances using +new or +alloc. +instance sounds like it does the same thing as +new, create a fresh instance. +sharedInstance makes it clear that this is an instance shared by other callers.
As a side-note, I'd mention that many in the ObjC community have been gradually moving away from singletons in recent years because of the difficulties they create in testing, their creation of global mutable state, and general inflexibility. Manual dependency injection (i.e. just setting properties or passing parameters, not complex DI frameworks) has been growing in favor. This is far from universal, and I'm not claiming Singleton is an anti-pattern in ObjC, but I do encourage you to avoid Singleton when it is not a huge benefit. For years it was my go-to solution to many problems, but times and patterns change.
There is a widely used pattern to create singletons using dispatch_once. There is absolutely no reason whatsoever why anyone would create a singleton in any different way. Instead you want to use some deeply obscure technology (which for example doesn't work once you switch to Swift).
By the way, try calling some class method to configure your singleton before it is created. Doesn't work with +load.
By the way, your "usual way" isn't thread safe.

Swizzling a single instance, not a class

I have a category on NSObject which supposed to so some stuff. When I call it on an object, I would like to override its dealloc method to do some cleanups.
I wanted to do it using method swizzling, but could not figure out how. The only examples I've found are on how to replace the method implementation for the entire class (in my case, it would override dealloc for ALL NSObjects - which I don't want to).
I want to override the dealloc method of specific instances of NSObject.
#interface NSObject(MyCategory)
-(void)test;
#end
#implementation NSObject(MyCategory)
-(void)newDealloc
{
// do some cleanup here
[self dealloc]; // call actual dealloc method
}
-(void)test
{
IMP orig=[self methodForSelector:#selector(dealloc)];
IMP repl=[self methodForSelector:#selector(newDealloc)];
if (...) // 'test' might be called several times, this replacement should happen only on the first call
{
method_exchangeImplementations(..., ...);
}
}
#end
You can't really do this since objects don't have their own method tables. Only classes have method tables and if you change those it will affect every object of that class. There is a straightforward way around this though: Changing the class of your object at runtime to a dynamically created subclass. This technique, also called isa-swizzling, is used by Apple to implement automatic KVO.
This is a powerful method and it has its uses. But for your case there is an easier method using associated objects. Basically you use objc_setAssociatedObject to associate another object to your first object which does the cleanup in its dealloc. You can find more details in this blog post on Cocoa is my Girlfriend.
Method selection is based on the class of an object instance, so method swizzling affects all instances of the same class - as you discovered.
But you can change the class of an instance, but you must be careful! Here is the outline, assume you have a class:
#instance MyPlainObject : NSObject
- (void) doSomething;
#end
Now if for just some of the instances of MyPlainObject you'd like to alter the behaviour of doSomething you first define a subclass:
#instance MyFancyObject: MyPlainObject
- (void) doSomething;
#end
Now you can clearly make instances of MyFancyObject, but what we need to do is take a pre-existing instance of MyPlainObject and make it into a MyFancyObject so we get the new behaviour. For that we can swizzle the class, add the following to MyFancyObject:
static Class myPlainObjectClass;
static Class myFancyObjectClass;
+ (void)initialize
{
myPlainObjectClass = objc_getClass("MyPlainObject");
myFancyObjectClass = objc_getClass("MyFancyObject");
}
+ (void)changeKind:(MyPlainObject *)control fancy:(BOOL)fancy
{
object_setClass(control, fancy ? myFancyObjectClass : myPlainObjectClass);
}
Now for any original instance of MyPlainClass you can switch to behave as a MyFancyClass, and vice-versa:
MyPlainClass *mpc = [MyPlainClass new];
...
// masquerade as MyFancyClass
[MyFancyClass changeKind:mpc fancy:YES]
... // mpc behaves as a MyFancyClass
// revert to true nature
[MyFancyClass changeKind:mpc: fancy:NO];
(Some) of the caveats:
You can only do this if the subclass overrides or adds methods, and adds static (class) variables.
You also need a sub-class for ever class you wish to change the behaviour of, you can't have a single class which can change the behaviour of many different classes.
I made a swizzling API that also features instance specific swizzling. I think this is exactly what you're looking for: https://github.com/JonasGessner/JGMethodSwizzler
It works by creating a dynamic subclass for the specific instance that you're swizzling at runtime.

How to instantiate a class in Objective-C that don't inherit from NSObject

Given this:
Person.h:
#interface Person
{
}
- (void) sayHello;
#end
Person.m:
#import "Person.h"
#implementation Person
- (void)sayHello
{
printf("%s", "Steve");
}
#end
How do you instantiate the Person? I tried this:
Person *p = [Person new];
That doesn't work, nor this:
Person *p = [Person alloc];
[UPDATE]
I forgot to tell, I already tried inheriting from NSObject, the new and alloc works. I'm just curious if we can instantiate a class that doesn't inherit from NSObject?
You absolutely can do so. Your class simply needs to implement +alloc itself, the way that NSObject does. At base, this just means using malloc() to grab a chunk of memory big enough to fit the structure defining an instance of your class.
Reference-counted memory management would also be nice (retain/release); this is actually part of the NSObject protocol. You can adopt the protocol and implement these methods too.
For reference, you can look at the Object class, which is a root ObjC class like NSObject, that Apple provides in its open source repository for the Objective-C runtime:
#implementation Object
// Snip...
+ alloc
{
return (*_zoneAlloc)((Class)self, 0, malloc_default_zone());
}
// ...
- init
{
return self;
}
// And so on...
That being said, you should think of NSObject as a integral part of the ObjC runtime. There's little if any reason to implement your own root class outside of curiosity, investigation, or experimentation (which should, however, not be discouraged at all).
You must:
Inherit from NSObject,
Do a "poor man's" class with your own mallocs, etc, or
Use Objective-C++ and create a C++ class.
Of course, neither of the other two fit into Objective-C storage management, and their call protocols, etc, are different.
There is (very likely) no good reason to not want to inherit from NSObject, but there are many good reasons to do so.
I would be curious as to your reason for why you don't want to inherit from NSObject. I would guess it stems from a lack of knowledge rather than a real need.
But even without knowing that reason: Don't do it. It's so hard to do this well in a way that it still plays nice with other Objective-C classes as to be virtually impossible.
Anyway, you're instantiating your objects in a way that hides what's really done. While in Java, you usually create instances via the default constructor method new, in Objective-C you instantiate by calling alloc on the class and then init on the instance:
Person *aPerson = [[Person alloc] init];
(It is possible to just use Person new, but I wouldn't do it because it hides what's really done from you)
You implement your class such that you inherit from NSObject and then, if necessary, write your own init method.
If you want to log to the console, use NSLog:
NSLog(#"Hello %#", #"Steven");
(#"" is a special constructor for a NSString. Strings in Objective-C are not byte arrays, but objects.)
you can't..
Alloc and new ..copy init all these methods are defined in NSObject..
You cannot also create your own since apple does not provide NSObject implementation class..so you have to inherit from NSObject or its subclass so that you can initialize your class

What's the difference between "class method" and "static method"?

I've worked with a few different languages such as Java, C#, and Objective-C.
In most languages, methods that don't require an instance of an object are called static methods. However, when it comes to Objective-C, some people get defensive when you call them static methods, and they expect you to call them class methods.
Why are they called class methods instead of static methods? What is the difference between a static method and a class method?
So my question is why are they called class methods instead of a
static method? What is the difference between a static method and a
class method?
From Wikipedia: Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance.
This describes exactly what Objective-C's class methods are not.
An Objective-C class method very much requires an instance that is the target of the method invocation. That is, it requires an instance of the metaclass that describes the class object being invoked.
Unlike static methods, Objective-C's class methods can be inherited (which, in combination with having the aforementioned self, is exactly why many classes can share a single, simple, implementation of +alloc on NSObject without needing their own custom implementations) and invoking a class method goes through the exact same objc_msgSend* based dispatch mechanism as any other method call site.
Objective-C's class methods can be overridden across the class hierarchy and they can be swizzled. None of which is supported in languages that typically offer static methods in lieu of class methods.
The bottom line is that static methods and class methods are very different. While that difference is mostly transparent for day to day coding purposes, there are still situations where knowing how class methods work can save you a ton of unnecessary lines of code.
For example, you can't do this with static methods:
#interface AbstractClass:NSObject
+ factory;
#end
#implementation AbstractClass
+ factory
{
return [[[self alloc] init] autorelease];
}
#end
#interface Concrete1:AbstractClass
#end
#implementation Concrete1
#end
#interface Concrete2:AbstractClass
#end
#implementation Concrete2
#end
void foo() {
Concrete1 *c = [Concrete1 factory];
Concrete2 *d = [Concrete2 factory];
... etc ...
}
Because it's dynamically bound, not static.
Because it's really a class object's instance method.
Objective-C class method is actually an object's class object's instance method.
It's hard to describe with text. See nice illustration here.
http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html
Though class methods and static methods are in practice the same most of the time, they are different. With static methods the class is acting as a namespace qualifier. With class methods the class itself is an object and so class methods are to the class object exactly the same thing instance methods are to an instance; as a consequence you can do the following
#interface TestClass : NSObject
+ (void)classOrInstanceMethod;
- (void)classOrInstanceMethod;
#end
...
NSArray * arr = [NSArray arrayWithObjects:
[[[TestClass alloc] init] autorelease],
[TestClass class],
nil];
for( id obj in arr )
[obj classOrInstanceMethod];
which version of classOrInstanceMethod is called depends on whether obj is a class object or and instance. If you are familiar with the factory class pattern, this pattern is part of the Objective-C language.
This is purely a historical difference, mostly stemming from the fact that Objective-C was developed contemporaneously with C++, and before C++ or later languages like Java and C# had much influence. Objective-C was essentially a port of the Smalltalk object model to C, so its syntax and terminology don't necessarily seem as "C-like" as that used by C++. However, Objective-C was in no way bucking a trend by not using the term "static method", because that trend wasn't well-established back in 1983.

What is the difference between class and instance methods?

What's the difference between a class method and an instance method?
Are instance methods the accessors (getters and setters) while class methods are pretty much everything else?
Like most of the other answers have said, instance methods use an instance of a class, whereas a class method can be used with just the class name. In Objective-C they are defined thusly:
#interface MyClass : NSObject
+ (void)aClassMethod;
- (void)anInstanceMethod;
#end
They could then be used like so:
[MyClass aClassMethod];
MyClass *object = [[MyClass alloc] init];
[object anInstanceMethod];
Some real world examples of class methods are the convenience methods on many Foundation classes like NSString's +stringWithFormat: or NSArray's +arrayWithArray:. An instance method would be NSArray's -count method.
All the technical details have been nicely covered in the other answers. I just want to share a simple analogy that I think nicely illustrates the difference between a class and an instance:
A class is like the blueprint of a house: You only have one blueprint and (usually) you can't do that much with the blueprint alone.
An instance (or an object) is the actual house that you build based on the blueprint: You can build lots of houses from the same blueprint. You can then paint the walls a different color in each of the houses, just as you can independently change the properties of each instance of a class without affecting the other instances.
Like the other answers have said, instance methods operate on an object and has access to its instance variables, while a class method operates on a class as a whole and has no access to a particular instance's variables (unless you pass the instance in as a parameter).
A good example of an class method is a counter-type method, which returns the total number of instances of a class. Class methods start with a +, while instance ones start with an -.
For example:
static int numberOfPeople = 0;
#interface MNPerson : NSObject {
int age; //instance variable
}
+ (int)population; //class method. Returns how many people have been made.
- (id)init; //instance. Constructs object, increments numberOfPeople by one.
- (int)age; //instance. returns the person age
#end
#implementation MNPerson
- (id)init{
if (self = [super init]){
numberOfPeople++;
age = 0;
}
return self;
}
+ (int)population{
return numberOfPeople;
}
- (int)age{
return age;
}
#end
main.m:
MNPerson *micmoo = [[MNPerson alloc] init];
MNPerson *jon = [[MNPerson alloc] init];
NSLog(#"Age: %d",[micmoo age]);
NSLog(#"%Number Of people: %d",[MNPerson population]);
Output:
Age: 0
Number Of people: 2
Another example is if you have a method that you want the user to be able to call, sometimes its good to make that a class method. For example, if you have a class called MathFunctions, you can do this:
+ (int)square:(int)num{
return num * num;
}
So then the user would call:
[MathFunctions square:34];
without ever having to instantiate the class!
You can also use class functions for returning autoreleased objects, like NSArray's
+ (NSArray *)arrayWithObject:(id)object
That takes an object, puts it in an array, and returns an autoreleased version of the array that doesn't have to be memory managed, great for temperorary arrays and what not.
I hope you now understand when and/or why you should use class methods!!
An instance method applies to an instance of the class (i.e. an object) whereas a class method applies to the class itself.
In C# a class method is marked static. Methods and properties not marked static are instance methods.
class Foo {
public static void ClassMethod() { ... }
public void InstanceMethod() { ... }
}
The answer to your question is not specific to objective-c, however in different languages, Class methods may be called static methods.
The difference between class methods and instance methods are
Class methods
Operate on Class variables (they can not access instance variables)
Do not require an object to be instantiated to be applied
Sometimes can be a code smell (some people who are new to OOP use as a crutch to do Structured Programming in an OO enviroment)
Instance methods
Operate on instances variables and class variables
Must have an instanciated object to operate on
I think the best way to understand this is to look at alloc and init. It was this explanation that allowed me to understand the differences.
Class Method
A class method is applied to the class as a whole. If you check the alloc method, that's a class method denoted by the + before the method declaration. It's a class method because it is applied to the class to make a specific instance of that class.
Instance Method
You use an instance method to modify a specific instance of a class that is unique to that instance, rather than to the class as a whole. init for example (denoted with a - before the method declaration), is an instance method because you are normally modifying the properties of that class after it has been created with alloc.
Example
NSString *myString = [NSString alloc];
You are calling the class method alloc in order to generate an instance of that class. Notice how the receiver of the message is a class.
[myString initWithFormat:#"Hope this answer helps someone"];
You are modifying the instance of NSString called myString by setting some properties on that instance. Notice how the receiver of the message is an instance (object of class NSString).
Class methods are usually used to create instances of that class
For example, [NSString stringWithFormat:#"SomeParameter"]; returns an NSString instance with the parameter that is sent to it. Hence, because it is a Class method that returns an object of its type, it is also called a convenience method.
So if I understand it correctly.
A class method does not need you to allocate instance of that object to use / process it. A class method is self contained and can operate without any dependence of the state of any object of that class. A class method is expected to allocate memory for all its own work and deallocate when done, since no instance of that class will be able to free any memory allocated in previous calls to the class method.
A instance method is just the opposite. You cannot call it unless you allocate a instance of that class. Its like a normal class that has a constructor and can have a destructor (that cleans up all the allocated memory).
In most probability (unless you are writing a reusable library, you should not need a class variable.
Instances methods operate on instances of classes (ie, "objects"). Class methods are associated with classes (most languages use the keyword static for these guys).
Take for example a game where lots of cars are spawned.. each belongs to the class CCar.
When a car is instantiated, it makes a call to
[CCar registerCar:self]
So the CCar class, can make a list of every CCar instantiated.
Let's say the user finishes a level, and wants to remove all cars... you could either:
1- Go through a list of every CCar you created manually, and do whicheverCar.remove();
or
2- Add a removeAllCars method to CCar, which will do that for you when you call [CCar removeAllCars]. I.e. allCars[n].remove();
Or for example, you allow the user to specify a default font size for the whole app, which is loaded and saved at startup.
Without the class method, you might have to do something like
fontSize = thisMenu.getParent().fontHandler.getDefaultFontSize();
With the class method, you could get away with [FontHandler getDefaultFontSize].
As for your removeVowels function, you'll find that languages like C# actually have both with certain methods such as toLower or toUpper.
e.g. myString.removeVowels() and String.removeVowels(myString) (in ObjC that would be [String removeVowels:myString]).
In this case the instance likely calls the class method, so both are available.
i.e.
public function toLower():String{
return String.toLower();
}
public static function toLower( String inString):String{
//do stuff to string..
return newString;
}
basically, myString.toLower() calls [String toLower:ownValue]
There's no definitive answer, but if you feel like shoving a class method in would improve your code, give it a shot, and bear in mind that a class method will only let you use other class methods/variables.
class methods
are methods which are declared as static. The method can be called without creating an instance of the class. Class methods can only operate on class members and not on instance members as class methods are unaware of instance members. Instance methods of the class can also not be called from within a class method unless they are being called on an instance of that class.
Instance methods
on the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword. Instance methods operate on specific instances of classes. Instance methods are not declared as static.
In Objective-C all methods start with either a "-" or "+" character.
Example:
#interface MyClass : NSObject
// instance method
- (void) instanceMethod;
+ (void) classMethod;
#end
The "+" and "-" characters specify whether a method is a class method or an instance method respectively.
The difference would be clear if we call these methods. Here the methods are declared in MyClass.
instance method require an instance of the class:
MyClass* myClass = [[MyClass alloc] init];
[myClass instanceMethod];
Inside MyClass other methods can call instance methods of MyClass using self:
-(void) someMethod
{
[self instanceMethod];
}
But, class methods must be called on the class itself:
[MyClass classMethod];
Or:
MyClass* myClass = [[MyClass alloc] init];
[myClass class] classMethod];
This won't work:
// Error
[myClass classMethod];
// Error
[self classMethod];
CLASS METHODS
A class method typically either creates a new instance of the class or retrieves some global properties of the class. Class methods do not operate on an instance or have any access to instance variable.
INSTANCE METHODS
An instance method operates on a particular instance of the class. For example, the accessors method that you implemented are all instance methods. You use them to set or get the instance variables of a particular object.
INVOKE
To invoke an instance method, you send the message to an instance of the class.
To invoke a class method, you send the message to the class directly.
Source: IOS - Objective-C - Class Methods And Instance Methods
Class methods can't change or know the value of any instance variable. That should be the criteria for knowing if an instance method can be a class method.
Also remember, the same idea applies to variables. You will come across terms like static, member, instance, class and so on when talking about variables the same as you would for methods/functions.
It seems the common term in the Obj-C community is ivar for instance variable, but I am not an Obj-C guy, yet.
An update to the above answers, I agree instance methods use an instance of a class, whereas a class method can be used with just the class name.
There is NO more any difference between instance method & class method after automatic reference counting came to existence in Objective-C.
For Example[NS StringWithformat:..] a class method & [[NSString alloc] initwihtformat:..] an instance method, both are same after ARC
Note: This is only in pseudo code format
Class method
Almost does all it needs to do is during compile time. It doesn't need any user input, nor the computation of it is based on an instance. Everything about it is based on the class/blueprint——which is unique ie you don't have multiple blueprints for one class. Can you have different variations during compile time? No, therefore the class is unique and so no matter how many times you call a class method the pointer pointing to it would be the same.
PlanetOfLiving: return #"Earth" // No matter how many times you run this method...nothing changes.
Instance Method
On the contrary instance method happens during runtime, since it is only then that you have created an instance of something which could vary upon every instantiation.
initWithName: #"John" lastName: #"Doe"Age:12 #"cool"
initWithName: #"Donald" lastName: #"Drumpf"Age:5 attitude:#"He started"
initWithName: #"President" lastName: #"Obama"Age:54 attitude: #"Awesome"
//As you can see the value can change for each instance.
If you are coming from other languages Static methods are same as class methods.
If you are coming from Swift, type methods are same as class methods.
Adding to above answers
Class method will work on class, we will use this for general purpose where like +stringWithFormat, size of class and most importantly for init etc
NSString *str = [NSString stringWithFormat:#"%.02f%%",someFloat];
Instance Method will work on an instance of a class not on a class like we are having two persons and we want to get know the balance of each separately here we need to use instance method. Because it won't return general response. e.g. like determine the count of NSSArray etc.
[johnson getAccountBalance];
[ankit getAccountBalance];