epic refactorings - melbourne cocoaheads june 2011
DESCRIPTION
By Jesse Collis and Luke Cunningham. June 2011TRANSCRIPT
epic refactorings andpatterns to makeyour code awesome
jesse collis (@sirjec)luke cunningham (@icaruswings)
firstly, read this book!
we did and itmade us betterengineers
insert a pic of usreading our bookshaving a‘lightbulb’ moment
we do agile...
write the minimumamount of code toimplement the story
12 months later...
1 small card
But that isn’twhat this talk isabout
just too much for oneview controller
the problem
First Diagram
alright stop!refactor time.
first problem
view controllers needonly worry aboutstandard UIViews andmanage the display.
coordination andcontainment!
ViewCoordinatorview lifecycle
@protocol ViewCoordinator <NSObject>
- (UIView *)view;
- (void)viewWillAppear;
- (void)viewDidAppear;
- (void)viewWillDisappear;
- (void)viewDidDisappear;
- (void)viewDidUnload;
- (void)didReceiveMemoryWarning;
@end
@interface YourViewController : UIViewController {
id<ViewCoordinator> viewCoordinator_;
}
...
@end
@implementation YourViewController
- (id)initWithNibName... { viewCoordinator_ = [[MyViewCoordinator alloc] init];}
- (void)loadView { [super loadView]; [self.view addSubview:[viewCoordinator_ view]];}
- (void)viewWillAppear { [[viewCoordinator_ view] viewWillAppear];}
...
@end
single responsibility
a class should onlyhave one reason tochange.
second problem
they’re notsimple views
ListCoordinatorbasic selectioncallbacks
@protocol ListCoordinator
- (void)onSelectListItem:^(id)block;
...
@end
@interface YourViewController : UIViewController {
id<ViewCoordinator> viewCoordinator_;
id<ViewCoordinator, ListViewCoordinator> listCoordinator_;
}
...
@end
@implementation YourViewController
- (id)initWithNibName... { listCoordinator_ = [[MyListCoordinator alloc] init];
[listCoordinator_ onSelectListItem:^(id listing) { [self showListing:listing]; }];}
- (void)showListing:(Listing *)listing;{
...
}
...
@end
interface segregation
clients should notbe forced to dependon methods theydon’t use.
QueryCoordinatorafter configure/cancel configurecallbacks
SearchBarCoord...
and so on...
now ourstructure is morelike this...
Go to Xcode
under the hood
EventDispatcher
@interface MapCooordinator <ViewCoordinator, ListCoordinator>{ ...}
@end
@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
there are nospecial cases
If you implementpatterns - follow your patterns
Coming in iOS5
thanks,jesse collis (@sirjec)luke cunningham (@icaruswings)