I searched on the Internet, trying to understand what these factory methods are, but I did not find a simple example showing a concrete example. One of the books that I briefly talk about them, but does not provide an example and does not provide an explanation of what they are. Class methods are commonly used as factory methods.
1. What is the syntax and use of the factory method in Objective-C? This is the closest answer I found, but when I tried the example in a comment compiled as answar, I got a message that I can’t call super. In this question, I'm more worried about the syntax for implementing the factory method.
2- Are there factory methods that are called constructors in other languages?
3 How do factory methods compare to singleton?
From the Apple documentation:
They combine selection and initialization in one step and return the created object
Isn't that what singleton does?
In the following singleton example, can we say that the method of the sharedData class is the factory method?
.m file
#import "SingletonModel.h"
@implementation SingletonModel
static SingletonModel *sharedData;
- (id) init {
if (self = [super init]) {
}
return self;
}
+(SingletonModel*) sharedData
{
if (!sharedData) {
sharedData = [[SingletonModel alloc]init];
}
return sharedData;
}
@end
source
share