epic refactorings and
patterns to make
your jesse collis (@sirjec)
      code awesome
     luke cunningham (@icaruswings)
firstly, read this book!
insert a pic of us
reading our books
having a
‘lightbulb’ moment



    we did and it
    made us better
    engineers
we do agile...
write the minimum
amount of code to
implement the story
12 months later...
1 small card
But that isn’t
what this talk is
about
the problem
just too much for one
view controller
First Diagram
alright stop!
refactor time.
first problem
view controllers need
only worry about
standard UIViews and
manage the display.
coordination and
containment!
ViewCoordinator
view 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 only
have one reason to
change.
second problem
they’re not
simple views
ListCoordinator
basic selection
callbacks
@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 not
be forced to depend
on methods they
don’t use.
QueryCoordinator
after configure/
cancel configure
callbacks
SearchBarCoord...



and so on...
now our
structure is more
like 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 no
special cases
If you implement
patterns -
follow your patterns
Coming in iOS5
thanks,
  jesse collis (@sirjec)
  luke cunningham (@icaruswings)

Epic Refactorings - Melbourne Cocoaheads June 2011