Two-StageCreation
Upcoming SlideShare
Loading in...5
×
 

Two-StageCreation

on

  • 683 views

 

Statistics

Views

Total Views
683
Slideshare-icon Views on SlideShare
683
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Two-StageCreation Two-StageCreation Presentation Transcript

  • Two-Stage Allocation / Template Methods
  • Two-Stage Creation
  • MyClass *class = [[MyClass alloc] init]; not MyClass *class = [MyClass new];
  • Subclassing-(id) initWithCenter:(CGPoint) aPoint { self = [super init]; if (nil != self) { center = aPoint; } return self; } -(id) init { return [self initWithCenter:CGPointZero]; }
  • Each class should have one designatedinitializer.Child classes should call it.Without Two-Stage creation, subclasseswould have to re-implement eachinitializer. (Think of NSData or NSString,which have many initializers...subclassingeach one would be a lot of work.)
  • Temporary Instances+(id)circleWithCenter:(CGPoint) aPoint { return [[[self alloc] initWithCenter:aPoint] autorelease]; }
  • Template Methods
  • Methods which are automatically called by the code but may not do anything. The programmer is expected to override them with some functionality.
  • Can you think of any examples?
  • • Identify steps of an algorithm and one or more methods to implement each step.
  • • Identify steps of an algorithm and one or more methods to implement each step.• Implement the algorithm as a sequence of calls to the identified methods.
  • • Identify steps of an algorithm and one or more methods to implement each step.• Implement the algorithm as a sequence of calls to the identified methods.• Factor out the customizable steps of the algorithm into Template Methods and provide a reasonable default implementation for each Template Method.
  • • Identify steps of an algorithm and one or more methods to implement each step.• Implement the algorithm as a sequence of calls to the identified methods.• Factor out the customizable steps of the algorithm into Template Methods and provide a reasonable default implementation for each Template Method.• Document whether the base class’ implementation of each Template Method can, should or must be called by subclasses that override them.
  • Caution
  • Caution-(void)viewWillAppear: (BOOL) animated {
  • Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;
  • Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}
  • Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}
  • Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated {
  • Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;
  • Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”; [super viewWillAppear:animated];
  • Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”; [super viewWillAppear:animated];}