Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax and use of a factory method in Objective-C

I have been searching the web trying to understand what factory methods are but I haven't found any simple example that shows a concrete example. One of the books I have briefly talks about them but it doesn't show an example nor gives an explanation of what they are "Class methods are commonly used as factory methods".

1- What is the syntax and use of a factory method in Objective-C? This is the closest answer I found but when I tried the example in the comment maked as the answar I got a message saying that I couldn't call super. In this question I'm more concern about the syntax of the implementation of the factory method.

2- Are factory methods what are called constructors in other languages?

3- How are factory methods compared to Singletons?

From Apple documentation:

They combine allocation and initialization in one step and return the created object

Is not what singleton does?

In the following singleton example, can we say that the class method sharedData is a factory method?

.m File

#import "SingletonModel.h"
@implementation SingletonModel
static SingletonModel *sharedData;

- (id) init {
    if (self = [super init]) {
        // custom initialization
    }
    return self;
}

 // is this a factory method?
+(SingletonModel*) sharedData
{
    if (!sharedData) {
        sharedData = [[SingletonModel alloc]init];
    }
    return sharedData;
}
@end
like image 619
fs_tigre Avatar asked Jan 18 '26 23:01

fs_tigre


2 Answers

People may use the same term for different things. Usually you create an object by calling

MyClass* object = [[MyClass alloc] initWithParameters... ];

You know you get an object of the class MyClass (which is not quite true in Objective C, but mostly). Now lets say there are a few subclasses of MyClass. As the caller, you don't know and don't care about these subclasses. If object was of class MySubClass, you wouldn't know and wouldn't care. But the implementer of MyClass cares. He or she created subclasses that work better for different purposes. So he creates a factory method and you call it like this:

MyClass* object = [MyClass objectForParameters:... ];

The class method objectForParameters looks at the parameters and decides which kind of object to return. The code might look like

if (...)
    return [[MySubClass1 alloc] initWithParameters:...];
else if (...)
    return [[MySubClass2 alloc] initWithParameters:...];
else
    return [[MySubClass3 alloc] initWithParameters:...];

You don't know what kind of object you get, but you know that you can treat it as if it belonged to the class MyClass. That kind of method is usually called a "factory method". It is a factory making objects for you. It decides which kind of object you get, not you.

like image 119
gnasher729 Avatar answered Jan 20 '26 12:01

gnasher729


What is the syntax and use of a factory method in Objective-C

If we take UIColor as an example, factory methods would be + (UIColor *)blackColor, + (UIColor *)clearColor, ...

From the other question you reference, any init... method should be an instance method (- (...), not + (...)). In that answer it is a class method and it shouldn't be.

Are factory methods what are called constructors in other languages

They all have the same purpose. Not all languages differentiate between allocation of memory and initialisation of that memory.

How are factory methods compared to Singletons

A singleton usually offers a single method to return the single instance of the class. It isn't strictly a factory method as it doesn't create a different instance each time it's called, but it is the same kind of thing.

like image 41
Wain Avatar answered Jan 20 '26 12:01

Wain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!