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

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]) {
        // custom initialization
    }
    return self;
}

 // is this a factory method?
+(SingletonModel*) sharedData
{
    if (!sharedData) {
        sharedData = [[SingletonModel alloc]init];
    }
    return sharedData;
}
@end
+3
source share
4 answers

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

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

From another question you refer, any method init...should be an instance method ( - (...), not + (...)). In this answer, this is a class method, and this should not be.

Are factory methods the so-called constructors in other languages

. .

factory Singletons

. factory, , .

+4

. ,

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

, MyClass ( Objective C, ). , MyClass. , . MySubClass, . MyClass. , . factory, :

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

objectForParameters , .

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

, , , , MyClass. "factory". factory . , , .

+5

A factory - , .
: UIColor , , iOS MacOS. MacOS NSColor, iOS UIColor. factory, .

(id)colorWithType:(ColorType)type
{
  id color;
    switch(type) {
      case type1:
       // create the colour for type1
       break;
  case type2:
      // create the colour for type2
      break;

  // now check the platform 
  if([self isCurrentPlatformMacOS]){
       // convert color to NSColor
  } else {
       // convert color to UIColor
  }
  return color;
  }
}

Watch this video, it will greatly facilitate understanding. http://www.youtube.com/watch?v=AsfM6YLtu9g (Go at 3:00)

+1
source

Design patterns can help you solve complex validation problems with proven solutions, and A factory provides one creation point for one or a group of objects. It also helps not to code a particular class. Good example and syntax

0
source