Two-StageCreation

750 views

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
750
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Two-StageCreation

    1. 1. Two-Stage Allocation / Template Methods
    2. 2. Two-Stage Creation
    3. 3. MyClass *class = [[MyClass alloc] init]; not MyClass *class = [MyClass new];
    4. 4. Subclassing-(id) initWithCenter:(CGPoint) aPoint { self = [super init]; if (nil != self) { center = aPoint; } return self; } -(id) init { return [self initWithCenter:CGPointZero]; }
    5. 5. 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.)
    6. 6. Temporary Instances+(id)circleWithCenter:(CGPoint) aPoint { return [[[self alloc] initWithCenter:aPoint] autorelease]; }
    7. 7. Template Methods
    8. 8. Methods which are automatically called by the code but may not do anything. The programmer is expected to override them with some functionality.
    9. 9. Can you think of any examples?
    10. 10. • Identify steps of an algorithm and one or more methods to implement each step.
    11. 11. • 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.
    12. 12. • 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.
    13. 13. • 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.
    14. 14. Caution
    15. 15. Caution-(void)viewWillAppear: (BOOL) animated {
    16. 16. Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;
    17. 17. Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}
    18. 18. Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}
    19. 19. Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated {
    20. 20. Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;
    21. 21. Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”; [super viewWillAppear:animated];
    22. 22. Caution-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”;}-(void)viewWillAppear: (BOOL) animated { myVariable = @”NewValue”; [super viewWillAppear:animated];}

    ×