简体   繁体   English

在Objective-C中,Factory Method和Convenience Initializer有什么区别?

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

So every object has the default initializer method, -init 所以每个对象都有默认的初始化方法-init

If you need your object to be instantiated with properties, you would write something like 如果你需要用属性实例化你的对象,你会写类似的东西
-initWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2

This would be called like this: 这将被称为这样:
[[SomeClass alloc]initWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2]

I always thought this was called a factory method (is that correct?) 我一直认为这被称为工厂方法(这是正确的吗?)

But then I stumbled across this SO question: How to write an Objective-C convenience constructor 但后来我偶然发现了这个问题: 如何编写Objective-C便利构造函数

Where it looks like a "convenience constructor" is the same thing as a factory method? 看起来像“便利构造函数”和工厂方法一样吗? But maybe a convenience constructor specifically uses a class method as the initializer? 但也许一个便利构造函数专门使用类方法作为初始化器? so it seems like a convenience constructor would look like this: 所以看起来像一个方便的构造函数看起来像这样:
+someClassWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2 and would be called like: +someClassWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2 ,将被调用为:
[SomeClass someClassWithProperty1:(Property1*)prop1 andProperty2:(Property2*)prop2];

Does anybody know what the terminology should actually be? 有人知道术语应该是什么吗? Are the two terms, "Factory Method" and "Convenience Constructor" the same thing in this context? 在这种情况下,“工厂方法”和“便利构造函数”这两个术语是否相同?

"Convenience constructor" used to be the official term that Apple used for this concept; “便利构造”曾经是Apple用于这个概念的官方术语; then they started calling it a "factory method" . 然后他们开始称它为“工厂方法” The nature of it hasn't changed: it's a class method that creates an instance . 它的本质没有改变:它是一个创建实例的方法 initWith... is not a factory method. initWith...不是工厂方法。

"Convenience initializer " is a term from Swift, not ObjC. “方便初始化程序 ”是来自Swift的术语,而不是ObjC。

The "designated initializer" is unrelated except insofar as this creation method, like any other*, must eventually call through to it. “指定的初始化程序”是不相关的,除非这种创建方法与任何其他*一样,最终必须调用它。


*With the exception of initWithCoder: *除了initWithCoder:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是便利初始化程序,因为它与Objective-C有关? - What is a convenience initializer as it pertains to Objective-C? Objective-C中的便捷初始化器 - Convenience initializer in objective-c 将Swift便捷初始化程序转换为Objective-C - Converting Swift convenience initializer to Objective-C 初始化和类工厂方法中的Objective-C .mm - Objective-C .mm in initializer and class factory method Objective-C中方法签名和选择器之间有什么区别? - What's the difference between the method signature and the selector in Objective-C? 为在Swift中使用的Objective-C类添加便捷的初始化程序扩展 - Adding an convenience initializer extension for Objective-C class to be used in Swift 是否可以在Objective-C中调用Swift便捷初始化程序 - Is it possible to call Swift convenience initializer in Objective-C Objective-C函数和Objective-C方法之间有什么区别? - What is the difference between an Objective-C function and an Objective-C method? 目标c中的工厂设计模式与工厂方法设计模式和抽象工厂之间有什么区别? - What is the difference between Factory design pattern from Factory method design pattern and abstract factory in objective c? Objective-C工厂方法如何转换为Swift便捷初始化器? - How are Objective-C factory methods converted into Swift convenience initializers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM