epic refactorings - melbourne cocoaheads june 2011

41
epic refactorings and patterns to make your code awesome jesse collis (@sirjec) luke cunningham (@icaruswings)

Upload: jesse-collis

Post on 01-Jul-2015

195 views

Category:

Technology


1 download

DESCRIPTION

By Jesse Collis and Luke Cunningham. June 2011

TRANSCRIPT

Page 1: Epic Refactorings - Melbourne Cocoaheads June 2011

epic refactorings andpatterns to makeyour code awesome

jesse collis (@sirjec)luke cunningham (@icaruswings)

Page 2: Epic Refactorings - Melbourne Cocoaheads June 2011

firstly, read this book!

Page 3: Epic Refactorings - Melbourne Cocoaheads June 2011

we did and itmade us betterengineers

insert a pic of usreading our bookshaving a‘lightbulb’ moment

Page 4: Epic Refactorings - Melbourne Cocoaheads June 2011

we do agile...

Page 5: Epic Refactorings - Melbourne Cocoaheads June 2011
Page 6: Epic Refactorings - Melbourne Cocoaheads June 2011

write the minimumamount of code toimplement the story

Page 7: Epic Refactorings - Melbourne Cocoaheads June 2011

12 months later...

Page 8: Epic Refactorings - Melbourne Cocoaheads June 2011

1 small card

Page 9: Epic Refactorings - Melbourne Cocoaheads June 2011
Page 10: Epic Refactorings - Melbourne Cocoaheads June 2011

But that isn’twhat this talk isabout

Page 11: Epic Refactorings - Melbourne Cocoaheads June 2011

just too much for oneview controller

the problem

Page 12: Epic Refactorings - Melbourne Cocoaheads June 2011

First Diagram

Page 13: Epic Refactorings - Melbourne Cocoaheads June 2011

alright stop!refactor time.

Page 14: Epic Refactorings - Melbourne Cocoaheads June 2011

first problem

Page 15: Epic Refactorings - Melbourne Cocoaheads June 2011

view controllers needonly worry aboutstandard UIViews andmanage the display.

Page 16: Epic Refactorings - Melbourne Cocoaheads June 2011

coordination andcontainment!

Page 17: Epic Refactorings - Melbourne Cocoaheads June 2011

ViewCoordinatorview lifecycle

Page 18: Epic Refactorings - Melbourne Cocoaheads June 2011

@protocol ViewCoordinator <NSObject>

- (UIView *)view;

- (void)viewWillAppear;

- (void)viewDidAppear;

- (void)viewWillDisappear;

- (void)viewDidDisappear;

- (void)viewDidUnload;

- (void)didReceiveMemoryWarning;

@end

Page 19: Epic Refactorings - Melbourne Cocoaheads June 2011

@interface YourViewController : UIViewController {

id<ViewCoordinator> viewCoordinator_;

}

...

@end

Page 20: Epic Refactorings - Melbourne Cocoaheads June 2011

@implementation YourViewController

- (id)initWithNibName... { viewCoordinator_ = [[MyViewCoordinator alloc] init];}

- (void)loadView { [super loadView]; [self.view addSubview:[viewCoordinator_ view]];}

- (void)viewWillAppear { [[viewCoordinator_ view] viewWillAppear];}

...

@end

Page 21: Epic Refactorings - Melbourne Cocoaheads June 2011

single responsibility

a class should onlyhave one reason tochange.

Page 22: Epic Refactorings - Melbourne Cocoaheads June 2011

second problem

Page 23: Epic Refactorings - Melbourne Cocoaheads June 2011

they’re notsimple views

Page 24: Epic Refactorings - Melbourne Cocoaheads June 2011

ListCoordinatorbasic selectioncallbacks

Page 25: Epic Refactorings - Melbourne Cocoaheads June 2011

@protocol ListCoordinator

- (void)onSelectListItem:^(id)block;

...

@end

Page 26: Epic Refactorings - Melbourne Cocoaheads June 2011

@interface YourViewController : UIViewController {

id<ViewCoordinator> viewCoordinator_;

id<ViewCoordinator, ListViewCoordinator> listCoordinator_;

}

...

@end

Page 27: Epic Refactorings - Melbourne Cocoaheads June 2011

@implementation YourViewController

- (id)initWithNibName... { listCoordinator_ = [[MyListCoordinator alloc] init];

[listCoordinator_ onSelectListItem:^(id listing) { [self showListing:listing]; }];}

- (void)showListing:(Listing *)listing;{

...

}

...

@end

Page 28: Epic Refactorings - Melbourne Cocoaheads June 2011

interface segregation

clients should notbe forced to dependon methods theydon’t use.

Page 29: Epic Refactorings - Melbourne Cocoaheads June 2011

QueryCoordinatorafter configure/cancel configurecallbacks

Page 30: Epic Refactorings - Melbourne Cocoaheads June 2011

SearchBarCoord...

and so on...

Page 31: Epic Refactorings - Melbourne Cocoaheads June 2011

now ourstructure is morelike this...

Page 32: Epic Refactorings - Melbourne Cocoaheads June 2011
Page 33: Epic Refactorings - Melbourne Cocoaheads June 2011

Go to Xcode

Page 34: Epic Refactorings - Melbourne Cocoaheads June 2011

under the hood

Page 35: Epic Refactorings - Melbourne Cocoaheads June 2011

EventDispatcher

Page 36: Epic Refactorings - Melbourne Cocoaheads June 2011

@interface MapCooordinator <ViewCoordinator, ListCoordinator>{ ...}

@end

Page 37: Epic Refactorings - Melbourne Cocoaheads June 2011

@implementation MapCoordinator

...

- (void)onSelectListItem:^(Listing *)block;{ [eventDispatcher_ on:@"select-listing” performWithObject:block];}

- (void)mapView:(... *)mapView didSelectAnnotationView:(... *)view;{ Listing *listing = ... find listing ... [eventDispatcher_ fire:@"select-listing" withObject:listing];}

...

@end

Page 38: Epic Refactorings - Melbourne Cocoaheads June 2011

there are nospecial cases

Page 39: Epic Refactorings - Melbourne Cocoaheads June 2011

If you implementpatterns - follow your patterns

Page 40: Epic Refactorings - Melbourne Cocoaheads June 2011

Coming in iOS5

Page 41: Epic Refactorings - Melbourne Cocoaheads June 2011

thanks,jesse collis (@sirjec)luke cunningham (@icaruswings)