Two-Stage Pattern & Initializers

Part 2 chapter 3 Two Stage patterns

[[SomeClass alloc] init].

 

 

Cocoa's NSObject base class provides two methods that allocate memory for new instances, +(id)alloc and +(id)allocWithZone:(NSZone *)aZone.

The +alloc method is implemented to call the +allocWithZone: method specifying a default zone argument.

The +alloc and +allocWithZone: methods each return a pointer to a newly allocated block of memory large enough to store an instance of the class that executed the method. The allocated memory contains zeros except for the one instacne variable isa that all Objective-C objects are required to have. The isa variable is automatically initialized to point to the class object that allocated the memory and is the tie-in to the Objective-C language runtime that enables the instance to receive message such as -init that are used to complete initialization.

 

 

Zone

Zones are used to make sure the memory allocated for objects that are used together is close together(OS 里面技术提过). When one of the objects is needed, the other is almost certainly also needed. Because the objects are in the same zone, the chances are good that all the needed objects are paged into memory at the same time, and when the objects are not needed, they are paged out together as well. Cocoa's NSZone type specifies a C structure that identifies a memoryfrom the specified zone.

 

// Designated Initializer
- (id)initWithCenter:(NSPoint)aPoint radius:(float)aRadius
{
    self = [super init];
    if(nil != self)
    {
        center = aPoint;
        radius = aRadius;
        label = [[NSString allocFromZone:[self zone]]
            initWithString:@”default”];
}

The zone used to allocate an object is determined by sending the -zone message to the object.      Each instance of MyCircle class above stores an NSString label allocated from the same zone as the instacnce itself.   

 

 

 

The Designated Initializer is usually the one that accepts the most arguments. All other initializers call the Designated Initializer in their implementations.

 Each class that introduces a new Designated Initializer must also override the inherited Designated Initializer to call the new one. For example, the MyCircle class introduces -initWithCenter:radius:, it must also implement -init to call -initWithCenter:radius;.

Guidelines for initializers when subclassing:

*  Make sure that the Designated Initializer calls its super class' implementation of the super class' Designated Initializer.

*  Assign self to the object returned by the superclass' Designated Initailizer.

*  Do not access instance variables if nil is returned by the superclass' Designated Initializer.

*  Make sure that the superclass' Designated Initializer is overridden to call the new Desinated Initializer.

*  When subclassing, make sure every new initializer that isn't the Designated Initialzer calls the Designated initializer. (including the inherited and the new initializers)

The benifit of the obve is that there is no need to override every inherited initializer when subclassing. (but the Designated Initializer in super class)

 

 Table Prominent Cocoa Classed and Their Designated Initializers

Class       Designated Initializer
NSObject     -init
NSView       -initWithFrame:
NSCell       -initImageCell: and -initTextCell:
NSControl      -initWithFrame:
NSDocument       -init
NSWindowController -initWithWindow:

 

 

 

Consequences

 

Separating the allocation and initialization stages of instance creation provides many benefits. It's possible to use any variation of the +alloc class method to allocate an instance and then use any avaiable initializer with the new instance. This makes it possible to create your own initialization methods without needing to provide alternate implementations of all allocation methods.

The separation simplifies the process of writhing initializers. Furthermore, Cocoa standard initializers like -initWithCoder: work with instances regardless of the way memory for the instance was allocated.

 

One negative consequence of the separation of allocation and initailization is the need to be aware of conventions such as the designated initializer.

 

 

 

 

 

 

posted on 2013-02-17 01:04  Chansonyan  阅读(203)  评论(0)    收藏  举报

导航