美文网首页
OC:生成器模式

OC:生成器模式

作者: 春暖花已开 | 来源:发表于2020-04-15 10:50 被阅读0次
    MZUser
    //.h
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @class MZUserBuilder ;
    @interface MZUser : NSObject
    
    @property(nonatomic, copy) NSString *userId;
    @property(nonatomic, copy) NSString *firstName;
    @property(nonatomic, copy) NSString *lastName;
    @property(nonatomic, copy) NSString *gender;
    @property(nonatomic, copy) NSDate *dateOfBirth;
    @property(nonatomic, strong) NSArray *albums;
    
    + (instancetype)userWithBlock:(void (^)(MZUserBuilder *))block;
    - (instancetype)initWithBuilder:(MZUserBuilder *)builder;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    //.m
    #import "MZUser.h"
    
    #import "MZUserBuilder.h"
    
    @implementation MZUser
    
    - (instancetype)initWithBuilder:(MZUserBuilder *)builder {
        if(self = [super init]) {
            self.userId = builder.userId;
            self.firstName = builder.firstName;
            self.lastName = builder.lastName;
            self.gender = builder.gender;
            self.dateOfBirth = builder.dateOfBirth;
            self.albums = [NSArray arrayWithArray:builder.albums];
        }
        return self;
    }
    
    + (instancetype)userWithBlock:(void (^)(MZUserBuilder *))block {
        MZUserBuilder *builder = [[MZUserBuilder alloc] init];
        block(builder);
        return [builder build];
    }
    
    
    @end
    
    MZUserBuilder
    //.h
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @class MZUser;
    @interface MZUserBuilder : NSObject
    
    @property(nonatomic, copy) NSString *userId;
    @property(nonatomic, copy) NSString *firstName;
    @property(nonatomic, copy) NSString *lastName;
    @property(nonatomic, copy) NSString *gender;
    @property(nonatomic, copy) NSDate *dateOfBirth;
    @property(nonatomic, strong) NSArray *albums;
    
    - (MZUser *)build;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    //.m
    #import "MZUserBuilder.h"
    #import "MZUser.h"
    
    @implementation MZUserBuilder
    
    - (MZUser *)build {
        return [[MZUser alloc] initWithBuilder:self];
    }
    
    @end
    

    使用
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    //    MZUser *user = [MZUser userWithBlock:^(MZUserBuilder * _Nonnull builder) {
    //        builder.userId = @"id001";
    //        builder.firstName = @"Alice";
    //        builder.lastName = @"Darji";
    //        builder.gender = @"F";
    //        NSCalendar *cal = [NSCalendar currentCalendar];
    //        NSDateComponents *components = [[NSDateComponents alloc] init];
    //        [components setYear:1980];
    //        [components setMonth:1];
    //        [components setDay:1];
    //        builder.dateOfBirth = [cal dateFromComponents:components];
    //        builder.albums = [NSArray array];
    //    }];
        
        MZUserBuilder *builder = [[MZUserBuilder alloc] init];
        builder.userId = @"id001";
        builder.firstName = @"Alice";
        builder.lastName = @"Darji";
        builder.gender = @"F";
        NSCalendar *cal = [NSCalendar currentCalendar];
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setYear:1990];
        [components setMonth:9];
        [components setDay:24];
        builder.dateOfBirth = [cal dateFromComponents:components];
        builder.albums = [NSArray array];
        MZUser *user = [[MZUser alloc] initWithBuilder:builder];
        
        NSLog(@"%@--%@--%@--%@--%@--%@", user.userId, user.firstName, user.lastName, user.gender, user.dateOfBirth, user.albums);
    }
    

    相关文章

      网友评论

          本文标题:OC:生成器模式

          本文链接:https://www.haomeiwen.com/subject/pfiqvhtx.html