Re: Objective-C basics - (Why NSNumber conforms to NSCopying protocol)
Re: Objective-C basics - (Why NSNumber conforms to NSCopying protocol)
- Subject: Re: Objective-C basics - (Why NSNumber conforms to NSCopying protocol)
- From: email@hidden
- Date: Fri, 12 Aug 2016 10:04:18 +0900
> On Aug 12, 2016, at 9:16 AM, Steve Sisak <email@hidden> wrote:
>
> There’s a standard idiom where immutable classes frequently have mutable subclasses (which expose the mutating methods).
>
> In this case, it’s common for the immutable (super)class to adopt NSCopying and the mutable subclass to also adopt NSMutableCopying.
>
> This way if you want to add an object to a collection, calling -copy has the effect of freezing the current value ― the immutable class can optimize this by just returning a new reference to the same object, but the caller doesn’t have to know whether the object was immutable or a mutable subclass.
Almost.
The design pattern for this is the class cluster.
Where the public interfaces may produce private subclasses that handle and respond appropriately to the messages that make them one or the other.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden