在 Objective-C 中,工厂方法和便利初始化器有什么区别?

In Objective-C, what is the difference between a Factory Method and a Convenience Initializer?

所以每个对象都有默认的初始化方法,-init

如果你需要用属性实例化你的对象,你可以这样写
-initWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2

可以这样称呼:
[[SomeClass alloc]initWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2]

我一直以为这叫工厂方法(对吗?)

但后来我偶然发现了这个 SO 问题: How to write an Objective-C convenience constructor

哪里看起来 "convenience constructor" 和工厂方法一样?但是也许便利的构造函数专门使用 class 方法作为初始化程序?所以看起来方便的构造函数看起来像这样:
+someClassWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2 会被称为:
[SomeClass someClassWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2];

有人知道术语实际上应该是什么吗? "Factory Method" 和 "Convenience Constructor" 这两个术语在这种情况下是一回事吗?

"Convenience constructor"曾经是Apple用于这个概念的官方术语;然后他们开始称它为 "factory method". The nature of it hasn't changed: it's a class method that creates an instanceinitWith... 不是工厂方法。

"Convenience initializer" 是来自 Swift 的术语,而不是 ObjC。

"designated initializer" 是不相关的,除了这个创建方法,像任何其他*一样,最终必须调用它。


*initWithCoder:

除外