类工厂方法的实现 [英] Class Factory Methods implementation

查看:76
本文介绍了类工厂方法的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在进入iphone开发之前,我正在研究Objective-C的Apple文档.练习之一指出,我应该创建一个指定的初始化程序(带有3个参数)和合适的工厂方法.

So I am working my way through the Apple documentation of objective-C (before jumping into iphone development). One of the exercises states that I should create a designated initializer (with 3 params) and suitable factory method.

现在我是基于我的理解来执行此操作的,但是我无法实现工厂方法,因为我不知道是否应该在实现中使用alloc和init?

Now I did this based on my understanding but I am unable to implement the factory method because I dont know if I should use alloc and init within its implementation or not?

练习:

声明并实现一个新的指定初始化器,该初始化器用于创建一个 XYZPerson使用指定的名字,姓氏和生日, 以及合适的类工厂方法.别忘了覆盖 初始化以调用指定的初始化程序.

Declare and implement a new designated initializer used to create an XYZPerson using a specified first name, last name and date of birth, along with a suitable class factory method. Don’t forget to override init to call the designated initializer.

代码:

//.h 

-(id)initWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob;

//.m
-(id)initWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *)dob{
    self = [super init];
    return [self initWithNameAndDob:fName last:lName birth:dob];
}

实施中缺少什么?

谢谢

推荐答案

声明并实现一个新的指定初始化器,该初始化器用于使用指定的名字,姓氏和出生日期来创建XYZPerson.

Declare and implement a new designated initializer used to create an XYZPerson using a specified first name, last name and date of birth...

您在声明中是正确的,但是您的实现是递归的,因为它是在调用自身.做类似的事情

You are correct in the declaration but your implementation is recursive, since it's calling itself. Do something like

//.h
-(id)initWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob;
//.m
-(id)initWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *)dob{
    if(self = [super init]) {
        // use the parameters to do something, eg.
        _fName = fName; // assuming you have an ivar called _fName 
        _lName = lName; // assuming you have an ivar called _lName
        _dob = dob; // assuming you have an ivar called _dob
    }
    return self;
}

然后

...以及合适的类工厂方法.

...along with a suitable class factory method.

工厂方法是产生对象实例的类方法.最常见的实现是让它分配和初始化该对象的新实例,然后将其返回.

A factory method is a class method that produces an instance of the object. The most common implementation is to have it to allocate and initialize a new instance of the object and return it.

//.h
+(instancetype)personWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob {


//.m
+(instancetype)personWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob {
return [[XYZPerson alloc] initWithNameAndDob:fName last:lName birth:dob];
}

最后

别忘了重写init来调用指定的初始化程序.

Don’t forget to override init to call the designated initializer.

由于您设计的初始值设定项是initWithNameAndDob:last:birth:,因此您的init实现必须调用它.设计的初始化程序的参数必须是合理的默认值,在这种情况下,nil很好.

Since your designed initializer is initWithNameAndDob:last:birth: your init implementation must call it. The parameters of the designed initializer have to be a reasonable default, in this case nil is fine.

-(id)init {
     return [self initWithNameAndDob:nil last:nil birth:nil];
 }

最后一点,我想指出的是,您对初始值设定项的命名约定不是那么好.

As a final remark I'd like to point out that your naming convention for the initializer is not that good. A more suitable and readable one would be

-(id)initWithFirstName:(NSString *)fName lastName:(NSString *)lName dateOfBirth:(NSDate *) dob;

这篇关于类工厂方法的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆