Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean "Architecutre" and Swift

Clean "Architecutre" and Swift

There is no place for small apps in the stores anymore. Nowadays mobile apps development requires a great amount of time and dedication, and getting your product to market quickly with high quality is a key factor for success. So the way we build app, the way we code is changing as well and one of the deepest changes will be how we will architect our code. In this session we will review most common architectural patterns and we will try to demonstrate their benefits in practice with Swift.

https://github.com/rafaelbartolome/SupReAct

Links:
MVP: http://martinfowler.com/eaaDev/uiArchs.html#Model-view-presentermvp
MVVM: https://www.objc.io/issues/13-architecture/mvvm/
Clean: https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
Viper: http://mutualmobile.github.io/blog/2013/12/04/viper-introduction/
Flux: https://facebook.github.io/flux/

Avatar for Rafael Bartolome

Rafael Bartolome

June 11, 2016
Tweet

Other Decks in Programming

Transcript

  1. (Wikipedia) Is both the process and the product of planning,

    designing, and constructing buildings and other physical structures. Architecture (Latin architectura, from the Greek ἀρχιτέκτων arkhitekton, from ἀρχι- "chief" and τέκτων "builder")
  2. Controller View Model Controller View Model Controller View Model Controller

    View M odel Controller View Model Controller View Model Controller View Model Controller View Model Controller View Model Controller View Model Controller View Model Controller View Model
  3. class HeroDetailsViewController: UIViewController { @IBOutlet var realNameLabel: UILabel! @IBOutlet var

    codeNameLabel: UILabel! @IBOutlet var pictureImageView: UIImageView! @IBOutlet var powersTextView: UITextView! ••• } MassiveVC solution Controller View Model
  4. override func viewDidLoad() { super.viewDidLoad() let userDefaults = NSUserDefaults.standardUserDefaults() realNameLabel.text

    = userDefaults.objectForKey("HeroRealName") as? String codeNameLabel.text = userDefaults.objectForKey("HeroCodeName") as? String if let imageName = userDefaults.objectForKey("HeroPicture") as? String { pictureImageView.image = UIImage(named: imageName) } powersTextView.text = userDefaults.objectForKey("HeroPowersDescription") as? String } MassiveVC solution Controller View Model
  5. protocol HeroDetailsView { func updateHero(name realName: String) func updateHero(alias codeName:

    String) func updateHero(picture imageName: String) func updateHero(powers powersDescription: String) } View Protocol Model Presenter View
  6. class HeroDetailsViewController: UIViewController { @IBOutlet var realNameLabel: UILabel! @IBOutlet var

    codeNameLabel: UILabel! @IBOutlet var pictureImageView: UIImageView! @IBOutlet var powersTextView: UITextView! lazy var presenter = HeroDetailsPresenter() override func viewDidLoad() { super.viewDidLoad() presenter.activate(with: self) } } View Controller Model Presenter View
  7. extension HeroDetailsViewController: HeroDetailsView { func updateHero(name realName: String) { realNameLabel.text

    = realName } func updateHero(alias codeName: String) { codeNameLabel.text = codeName } func updateHero(picture imageName: String) { pictureImageView.image = UIImage(named: imageName) } func updateHero(powers powersDescription: String) { powersTextView.text = powersDescription } } View Protocol Imp. Model Presenter View
  8. final class HeroDetailsPresenter { func activate(with view: HeroDetailsView) { view.updateHero(name:

    getRealName()) view.updateHero(alias: getCodeName()) view.updateHero(picture: getImage()) view.updateHero(powers: getPowers()) } } Presenter Model Presenter View
  9. final class HeroDetailsPresenter { //Dependencies lazy var realNameInteractor = GetRealName()

    func activate(with view: HeroDetailsView) { view.updateHero(name: getRealName()) ••• } } private extension HeroDetailsPresenter { func getRealName() -> String { return realNameInteractor.execute() } ••• } Presenter Data Provider Presenter View Entity Interactor
  10. final class GetRealName { //Dependecies lazy var heroRepository: HeroRepository lazy

    var decriptService: DecriptService func execute() -> String { let hero = heroRepository.nextHero() return decriptService.decript( heroName: hero.encriptedName) } } Interactor Data Provider Presenter View Entity Interactor
  11. struct Hero { private (set) var encriptedName: String private (set)

    var codeName: String private (set) var imageName: String private (set) var powersDescription:String init(name: String = "", codeName: String = "", imageName: String = "", powersDescription: String = "") { self.encriptedName = name self.codeName = codeName self.imageName = imageName self.powersDescription = powersDescription } } Entity Data Provider Presenter View Entity Interactor
  12. final class HeroRepositoryImp: HeroRepository { //Dependencies lazy var heroStorage =

    HeroStorage() lazy var remoteAPIService = ShieldAPIService() lazy var heroMapper = HeroMapper() init() { remoteAPIService.getHeroes{ [weak self] heroDictionary in if let hero = self?.heroMapper .process(heroDictionary) { self?.heroStorage.add(hero) } } } func nextHero() -> Hero { return heroStorage.nextHero() } } Repository Presenter View Interactor Repository