What is the purpose of use of a factory method in objective-c context?
I am a bit confused about the use of factory methods in objective-c? What is the usefulness of doing so?
What is an example of using factory method in objective-c?
A bit confused. Any explanation would help!
Objective-C does not have constructor methods that are available in other programming languages. Factory methods are essentially Objective C's constructor methods. They allow you to allocate memory for your object and initialize the values.
In your class, add methods with this pattern:
Then in main, you can do:
It saves you from having to do this:
Or this:
A factory method can be any class or instance method which returns a newly created and initialized object.
A "class factory method" in a more strict Objective-C context is any class method of a class Class whose name begins with class.., with its prefix stripped and first letter lowercased. Example for class
Foo
:One reason to use class factory methods is for convenience. If you have a class
Foo
it may be created as follows in the usual way:A convenient class factory method may now be used as this:
However, there are other reasons where a class factory method is useful:
Suppose for example that the
initWithParam:
method can fail. Then there is much confusion about how this should be handled: should one throw an exception within init? Or should one returnnil
? There is controversy debate about the right approach in the community. Both approaches have subtle issues, including memory leaks which can not be avoided.Thus, the most reasonable approach in such a case would be to use a class factory method with an error parameter. Here, you would get a Foo object as follows:
Now, the rule is: If foo is nil, there was an error, and one can obtain detailed info about the error with the output parameter error:
Typically, the implementation would look as below:
Another reason is to implement "class clusters". That basically means that a class factory method returns a subclass of the class. An example is the
NSStream
class.The
NSInputStream
class has a class factory method:This returns a specialized class of
NSInputStream
.In the pre-era of ARC there is an important detail:
Objects returned from a class factory method are 'autoreleased'. In other words, the client receiving the object does not own it - and thus does not need to release it.
Nowadays, with ARC these details became blurred: you do not need to release those objects manually anyway. How and when the object gets actually released is also subject to compiler optimizations.