美文网首页
第18条:尽量使用不可变对象

第18条:尽量使用不可变对象

作者: MrSYLong | 来源:发表于2018-09-14 23:16 被阅读6次

要点一:
在设计类的时候,我们会充分运用属性来封装数据,但应该尽量把对外公布的属性设置为只读,而且只在确有必要时才将属性对外公布。

// 举例:头文件中
#import <Foundation/Foundation.h>

@interface EOCPointOfInterest : NSObject

@property (nonatomic, copy, readonly) NSString *identifier;

@property (nonatomic, copy, readonly) NSString *title;

@property (nonatomic, assign, readonly) float latitude;

@property (nonatomic, assign, readonly) float longitude;

@end

要点二:
有时需要修改封装在对象内部的数据,不想令这些数据为外人所改动。我们可以将属性在对象内部重新声明为readwrite,这一操作可于“class-continuation分类”中完成,在公共接口中声明的属性可于此处重新声明,属性的其他特质必须保持不变,而readonly可以扩展为readwrite。

// 举例:实现文件中
#import "EOCPointOfInterest.h"

@interface EOCPointOfInterest()

@property (nonatomic, copy, readwrite) NSString *identifier;

@property (nonatomic, copy, readwrite) NSString *title;

@property (nonatomic, assign, readwrite) float latitude;

@property (nonatomic, assign, readwrite) float longitude;

@end

@implementation EOCPointOfInterest


@end

要点三:
对象表示的各种集合属性,应该提供一个readonly属性供外界使用,该属性将返回不可变的集合,而该集合是内部那个可变集合的一份拷贝。即不要把可变的集合作为属性公开,而应该提供相关方法,以此修改对象中的可变集合。

// 头文件:
#import <Foundation/Foundation.h>

@interface EOCPerson : NSObject

@property (nonatomic, copy, readonly) NSString *firstName;
@property (nonatomic, copy, readonly) NSString *lastName;
@property (nonatomic, copy, readonly) NSSet *friends;

- (id)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName;
- (void)addFriend:(EOCPerson *)person;
- (void)removeFriend:(EOCPerson *)person;

@end

// 实现文件:
#import "EOCPerson.h"

@interface EOCPerson()

@property (nonatomic, copy, readwrite) NSString *firstName;
@property (nonatomic, copy, readwrite) NSString *lastName;

@end

@implementation EOCPerson {
    NSMutableSet *_internalFriends;
}

- (NSSet *)friends
{
    return [_internalFriends copy];
}

 - (void)addFriend:(EOCPerson *)person
{
    [_internalFriends addObject:person];
}

- (void)removeFriend:(EOCPerson *)person
{
    [_internalFriends removeObject:person];
}

- (id)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName
{
    if (self = [super init]) {
        _firstName = firstName;
        _lastName = lastName;
        _internalFriends = [NSMutableSet new];
    }
    return self;
}

@end

相关文章

网友评论

      本文标题:第18条:尽量使用不可变对象

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