This is a follow-up to my post on why you should unit test Cocoa and i Phone applications.
Remember, TDD is about tests development; however you must remember to listen to the tests when they are crying out in pain.
I’m going to take Matt’s Mac and i Phone projects with unit tests and re-work them so that they are much more manageable and cleaner.
delegate that listens for updates and formats the location into HTML for the web view, strings labels for the buttons, and a Google Maps URL.
The code between these two apps is identical and is a good example of a violation of the Don’t Repeat Yourself, or DRY, Principle. Part of what makes this difficult is that it is highly coupled to the UI.
In the Mac app, this code lives in an in a Mac app.
The answer is to extract this code into a new class that can be used from both applications.
Because the primary function of this new class is to take core location updates and format them to various strings, I’m calling the class subclass, but let’s stick with it for now.
The sticking point is that we somehow need this new class to update the UI, irrespective of Cocoa vs. We need to break the direct dependency on Cocoa and Cocoa Touch, and I’ve chosen to use a delegate with a single method as a bit of dependency inversion: - (void)location Formatter:(My Core Location Formatter *)formatter did Update Formatted String:(NSString *)formatted String location Label:(NSString *)location Label accuracty Label:(NSString *)accuracy Label; @end to send out the update with the strings in the user info dictionary.
Or we could have properties for the three strings and allow interested parties to monitor their updates using key-value observing.