質問

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
役に立ちましたか?

解決 2

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.

他のヒント

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.

A factory method is a method that make all the common creation for an object and return it to you.
For example: you want a UIColor from a method, but you want your method to be cross platformed in iOS and MacOS. In this case for MacOS you need this method to return NSColor, and for iOS you need UIColor. So the best thing to do is to use Factory method that returns to you different object according to your platform.

(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 make it much easier to understand.
http://www.youtube.com/watch?v=AsfM6YLtu9g (Go to 3:00 time)

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top