Objective-C中工厂方法的语法和用法是什么

时间:2023-01-15 15:12:57

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".

我一直在web上搜索,试图了解工厂方法是什么,但我没有找到任何简单的示例来显示具体的示例。我曾在其中一本书中简要地讨论过它们,但它没有给出一个示例,也没有解释它们是什么“类方法通常被用作工厂方法”。

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.

1- Objective-C中工厂方法的语法和用法是什么?这是我找到的最接近的答案,但当我在评论中尝试这个例子时,我得到一条信息,说我不能调用super。在这个问题中,我更关心工厂方法实现的语法。

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

工厂方法在其他语言中被称为构造函数吗?

3- How are factory methods compared to Singletons?

3-与单件相比,工厂的方法是怎样的?

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?

在下面的单例示例中,我们是否可以说类方法sharedData是一个工厂方法?

.m File

m文件

#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

4 个解决方案

#1


4  

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

Objective-C中工厂方法的语法和用法是什么

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

如果我们以UIColor为例,工厂方法将是+ (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.

你提到的另一个问题,任何init。方法应该是实例方法(-(…),而不是+(…)。在这个答案中,它是一个类方法,不应该是。

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.

单例通常提供一个方法来返回类的单个实例。严格来说,它并不是一个工厂方法,因为每次调用它时它都不会创建一个不同的实例,但它是相同的东西。

#2


5  

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的一个对象(在Objective C中不是很正确,但大部分是)。现在假设MyClass有几个子类。作为调用者,您不知道也不关心这些子类。如果对象属于MySubClass,你不会知道,也不会在意。但是MyClass的实现者关心的是。他或她创建的子类可以更好地用于不同的目的。他创建了一个工厂方法,你这样称呼它:

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

类方法objectForParameters会查看参数并决定返回哪种对象。代码可能是这样的

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.

你不知道你得到了什么样的对象,但是你知道你可以把它当作属于类MyClass。这种方法通常被称为“工厂方法”。它是一个为你制造物品的工厂。它决定你得到哪种对象,而不是你。

#3


1  

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.

factory方法是为对象创建所有公共创建并将其返回给您的方法。例如:您需要一个方法的UIColor,但是您希望您的方法在iOS和MacOS中跨平台。在本例中,对于MacOS,您需要这个方法来返回NSColor,对于iOS,您需要UIColor。所以最好的方法是使用工厂方法,根据平台返回不同的对象。

(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)

看这个视频,会更容易理解。http://www.youtube.com/watch?v=AsfM6YLtu9g(时间3点)

#4


0  

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

设计模式可以帮助您使用经过验证的解决方案解决复杂的编码问题,工厂为一个或一组对象提供了一个创建点。它还有助于不对具体的类进行编码——很好的示例和语法

#1


4  

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

Objective-C中工厂方法的语法和用法是什么

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

如果我们以UIColor为例,工厂方法将是+ (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.

你提到的另一个问题,任何init。方法应该是实例方法(-(…),而不是+(…)。在这个答案中,它是一个类方法,不应该是。

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.

单例通常提供一个方法来返回类的单个实例。严格来说,它并不是一个工厂方法,因为每次调用它时它都不会创建一个不同的实例,但它是相同的东西。

#2


5  

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的一个对象(在Objective C中不是很正确,但大部分是)。现在假设MyClass有几个子类。作为调用者,您不知道也不关心这些子类。如果对象属于MySubClass,你不会知道,也不会在意。但是MyClass的实现者关心的是。他或她创建的子类可以更好地用于不同的目的。他创建了一个工厂方法,你这样称呼它:

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

类方法objectForParameters会查看参数并决定返回哪种对象。代码可能是这样的

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.

你不知道你得到了什么样的对象,但是你知道你可以把它当作属于类MyClass。这种方法通常被称为“工厂方法”。它是一个为你制造物品的工厂。它决定你得到哪种对象,而不是你。

#3


1  

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.

factory方法是为对象创建所有公共创建并将其返回给您的方法。例如:您需要一个方法的UIColor,但是您希望您的方法在iOS和MacOS中跨平台。在本例中,对于MacOS,您需要这个方法来返回NSColor,对于iOS,您需要UIColor。所以最好的方法是使用工厂方法,根据平台返回不同的对象。

(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)

看这个视频,会更容易理解。http://www.youtube.com/watch?v=AsfM6YLtu9g(时间3点)

#4


0  

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

设计模式可以帮助您使用经过验证的解决方案解决复杂的编码问题,工厂为一个或一组对象提供了一个创建点。它还有助于不对具体的类进行编码——很好的示例和语法