Factory Class Method Implementation

So, I'm working on Apple's objective-C documentation (before moving on to the development of iphone). In one of the exercises, it is indicated that I should create a designated initializer (with 3 parameters) and a suitable factory method.

Now I have done this based on my understanding, but I can’t implement the factory method because I don’t know whether I should use alloc and init in its implementation or not?

exercise:

Declare and implement the new designated initializer used to create XYZPerson using the specified first name, last name, and date of birth, along with a suitable factory class. Remember to override init to invoke the designated initializer.

The code:

//.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];
}

is there something missing in the implementation?

Thank you,

+2
source share
2

, XYZPerson , ...

, , . -

//.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;
}

... factory.

A factory - , . , .

//.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, .

initWithNameAndDob:last:birth:, init . , nil .

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

, .

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

, .

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

It should be replaced with 
+(instancetype)personWithNameAndDob:(NSString *)fName last:(NSString *)lName birth:(NSDate *) dob {
return [[self alloc] initWithNameAndDob:fName last:lName birth:dob];
}

: , , . Youll . , factory: [self alloc] : http://qualitycoding.org/factory-method/

+5

All Articles