Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Refactoring to avoid tight coupling #7

Open
wants to merge 3 commits into from

3 participants

@andyw8

This code demonstrates how BeerDetailsController can be injected as a dependency into BeerListController. This avoids the awkward chained call of BeerDetailsController via UIApplication (which violates the Law Of Demeter).

@andyw8 andyw8 Refactoring to avoid tight coupling
I've changed this to demonstrate how BeerDetailsController can be injected as a dependency into BeerListController. This avoids the awkward chained call via UIApplication (which violates the Law Of Demeter).
d874f69
Beers/app/beer_list_controller.rb
@@ -1,6 +1,7 @@
class BeerListController < UITableViewController
- def init
- if super
+ def init(beer_details_controller)
@lrz Owner
lrz added a note

Wouldn't it be clearer to define initWithDetailsController() instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andyw8

I agree initWithDetailsController would be a better name. I tried renaming it but it fails with:

"Terminating app due to uncaught exception 'ArgumentError',
reason: 'beer_list_controller.rb:4:in `initWithDetailsController:':
wrong number of arguments (0 for 1) (ArgumentError)"

Not sure how to deal that?

@lrz
Owner

Perhaps the 'super' call is the cause of the exception here? I would try replacing 'super' with 'init'.

@andyw8

That's seems a good solution.

@lrz
Owner

Now I think BeerMapController should also be similarly updated? (Sorry just realized this now!).

@andyw8

Hmm, that seems to break the the tab bar title/icon for some reason.

@alloy alloy commented on the diff
Beers/app/beer_list_controller.rb
@@ -1,6 +1,7 @@
class BeerListController < UITableViewController
- def init
- if super
+ def initWithDetailsController(beer_details_controller)
+ @beer_details_controller = beer_details_controller
@alloy Owner
alloy added a note

Storing instance variables is not supposed to be done until after the if init check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@alloy
Owner

Is the reason that the detail controller is being cached so that the webview doesn’t have to be loaded every time? Can we just instantiate it on demand every time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 12, 2012
  1. @andyw8

    Refactoring to avoid tight coupling

    andyw8 authored
    I've changed this to demonstrate how BeerDetailsController can be injected as a dependency into BeerListController. This avoids the awkward chained call via UIApplication (which violates the Law Of Demeter).
Commits on May 13, 2012
  1. @andyw8

    Better name for the initialiser

    andyw8 authored
Commits on May 14, 2012
  1. @andyw8
This page is out of date. Refresh to see the latest.
View
3  Beers/app/app_delegate.rb
@@ -2,7 +2,8 @@ class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
tabbar = UITabBarController.alloc.init
- tabbar.viewControllers = [BeerMapController.alloc.init, BeerListController.alloc.init]
+ beer_list_controller = BeerListController.alloc.initWithDetailsController(beer_details_controller)
+ tabbar.viewControllers = [BeerMapController.alloc.init, beer_list_controller]
tabbar.selectedIndex = 0
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(tabbar)
@window.rootViewController.wantsFullScreenLayout = true
View
10 Beers/app/beer_list_controller.rb
@@ -1,6 +1,7 @@
class BeerListController < UITableViewController
- def init
- if super
+ def initWithDetailsController(beer_details_controller)
+ @beer_details_controller = beer_details_controller
@alloy Owner
alloy added a note

Storing instance variables is not supposed to be done until after the if init check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if init
self.tabBarItem = UITabBarItem.alloc.initWithTitle('List', image:UIImage.imageNamed('list.png'), tag:1)
end
self
@@ -33,8 +34,7 @@ def tableView(tableView, cellForRowAtIndexPath:indexPath)
def tableView(tableView, accessoryButtonTappedForRowWithIndexPath:indexPath)
beer = Beer::All[indexPath.row]
- controller = UIApplication.sharedApplication.delegate.beer_details_controller
- navigationController.pushViewController(controller, animated:true)
- controller.showDetailsForBeer(beer)
+ navigationController.pushViewController(@beer_details_controller, animated:true)
+ @beer_details_controller.showDetailsForBeer(beer)
end
end
View
8 Beers/app/beer_map_controller.rb
@@ -1,6 +1,7 @@
class BeerMapController < UIViewController
- def init
- if super
+ def initWithDetailsController(beer_details_controller)
+ @beer_details_controller = beer_details_controller
+ if init
self.tabBarItem = UITabBarItem.alloc.initWithTitle('Map', image:UIImage.imageNamed('map.png'), tag:1)
end
self
@@ -43,8 +44,7 @@ def mapView(mapView, viewForAnnotation:beer)
def showDetails(sender)
if view.selectedAnnotations.size == 1
beer = view.selectedAnnotations[0]
- controller = UIApplication.sharedApplication.delegate.beer_details_controller
- navigationController.pushViewController(controller, animated:true)
+ navigationController.pushViewController(@beer_details_controller, animated:true)
controller.showDetailsForBeer(beer)
end
end
Something went wrong with that request. Please try again.