Kodeco Forums

RWDevCon 2016 Session 201: iOS Design Patterns in Swift

Tired of reinventing the wheel? In this session, you’ll learn about iOS design patterns: reusable solutions to commonly occurring problems. You’ll also learn new and impressive terms — “loose coupling,” “composition over inheritance,” “polymorphic design” and more sure to impress your friends and loved ones.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1298-rwdevcon-2016-session-201-ios-design-patterns-in-swift

The audio for this talk is really screwed up, with lots of echoes or “skips”. Is there any way for you to fix it?

I really like the facade pattern you demonstrate around 12:30 of the video. Because the facade is meant to hide all the complex logic within the facade’s dependencies such as the ‘builder’ object in the video, shouldn’t those objects be initialized privately in the facade model? It looks like you can only create the facade through injecting the builder and network dependency, which is supposedly housing complex logic. Wouldn’t you want the view controller to be completely unaware of those dependencies and expose only a simple interface— ‘getModels…’?

Thanks!

Yes, if you always used the same builder and networkManager, you could hide their instantiation within the facade’s initializer. This would indeed lead to a simpler initializer, init.

However, there’s a trade-off made here: what if you use multiple types of builder and networkManager objects? If so, you’d want to inject these instead.

The point is: protocols protect objects against change. If you need something to change (e.g. you use multiple or different instances at run time), depend on a protocol, not a concrete class implementation. This corresponds to the Dependency Inversion Principle.

By the way, in the example shown, the view controller that uses the facade doesn’t know about the facade’s dependencies- it actually doesn’t even know about the real facade, but rather, only a protocol.

Instead, whenever the view controller is setup, it has the facade injected into it (via setting its property).

Thereby, the facade would be setup by whatever object that sets up the view controller.

This would most sense if the user is required to make some choice that affects the facade setup.

For example, imagine a “movie service” that allows you to rent from different services-- Netflix, Redbox, whatever-- and on the first screen, you select which service you’d like to query.

In this case, it would make perfect sense for a SelectRentalServiceViewController to setup the facade, as it would have to know about the different concrete networkManager and builder objects based on the user’s selection.

Sorry, there were a few audio issues happening live during the presentation. This was the first talk of the conference, and the audio team was still getting everything worked out.

However, the audio does get better as the video goes on. It’s just the first part that’s a bit choppy…!

@jrg.developer , Thanks for the prompt response, That makes complete sense, and I wish I had paid more attention to the response you gave to one of the members in the audience asking about the facade example as both our questions are similar.
Great presentation Joshua!

I’m sorry, but the PDF says there is second part take-home challenge under the LAB Challenge directory, but I can find it nowhere. Could you please tell me how can I find the second part challenge, Thanks.

This is an error in the RWDevCon version 1.0 PDF, which has been corrected in the latest version. Please make sure you download the latest PDF (currently version 1.2), as there have been revisions since the conference.

To explain, we decided against having challenges this year as part of RWDevCon, as there was already a lot of material, and we were concerned about time constraints for each session. Thereby, the Lab Challenge was cut, but the text referring to it was erroneously included in the PDF used at the conference.

However, the challenge was related to MVVM, which there is an entire session about, Session 306: MVVM in Practice. So, if you do want another challenge, go through this session! ;]

I copy and pasted your facade design code into my own project. When I try and instantiate the director like so:

viewDidLoad{ director.getModels…

I get a nil director. How would I fix this?

@dubayb Thanks very much for your question, and my apologies for the delayed response.

I would recommend that you revisit the pattern, as well as others in our new book which we just recently released.

The book is very current, and provides in-depth explanations that you’ll find very useful. :slight_smile:

I hope this helps!

All the best!