ios architecture

24
iOS Architecture Essential Jiakai(Jacky) Lian Mobile Developer @ GoGet

Upload: jacky-lian

Post on 21-Jan-2017

125 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: iOS Architecture

iOS Architecture Essential

Jiakai(Jacky) Lian Mobile Developer @ GoGet

Page 2: iOS Architecture

About Me❖ Focus on mobile development

since the era of 2G❖ Master of Applied IT @ Monash❖ Not a full stack developer❖ Focus on iOS in recent 2 years❖ Recently Joined GoGet

CarSharing❖ LinkedIn:

https://au.linkedin.com/in/jacky-lian-06493379

❖ GitHub: https://github.com/jiakai-lian/

Page 3: iOS Architecture

Purpose of Architecture Design

❖ Resolve real business problems❖ Remove unnecessary dependencies❖ Reduce maintenance cost❖ Prepare for changes

Page 4: iOS Architecture

3 Main Roles in Architecture

❖ Data Owner❖ Data Processor❖ Data Consumer

Page 5: iOS Architecture

Everything starts from MVC

❖ MVC: Model+View+Controller

❖ MVC in Web programming❖ View Container: Web

Browser❖ Model: Model Layer

Page 6: iOS Architecture

What Apple promised

Page 7: iOS Architecture

The Reality

❖ MVC = Massive View Controller❖ View Controllers have too many responsibilities: Networking, Data

Access,UI configuration, Data Source, Delegate …❖ Shared Data causes problems (Mutability Management)❖ Controllers are the least reusable classes in the project❖ “Cocoa MVC is the best architectural pattern in terms of the speed of

the development.”

Page 8: iOS Architecture

Slim Model V.S. Fat Model

❖ Fat Model❖ Business Logic in Model

❖ Slim Model❖ Model + Helpers❖ MVC-S: MVC+Data Store(Data Access)❖ MVC-N: MVC+Networking

Page 9: iOS Architecture

The True MVC in Mobile: MVP

❖ MVP: Model+Passive View+Presenter❖ All UI logic in presenter.❖ View knows nothing about Model

Page 10: iOS Architecture

MVVM

❖ MVVM = Model+View+ViewModel❖ ViewModel Definition

Page 11: iOS Architecture

MVVM:View Model

❖ Non-Pattern ViewModel: ViewData/Transition Data (Immutable)

❖ ViewModel in MVVM: Independent Presentation Logic

Page 12: iOS Architecture

MVVM: Where is the Controller

❖ MVVM = M-VM-C-V = Model ViewModel Controller View❖ Controller bind View and ViewModel together

Page 13: iOS Architecture

Benefits of ViewModel

❖ No direct dependency between Model & View❖ UI Independent Presentation Logic

Page 14: iOS Architecture

MVVM With Binding

❖ Fine without binding ❖ Even better with binding❖ Popular Binding Frameworks:

ReactiveCocoa(ObjC & Swift), RxSwift❖ Reactive : Learning Cost ⬆ ❖ Binding: Debugging Difficulty⬆

Page 15: iOS Architecture

VIPER

❖ VIPER = View+Interactor+Presenter+Entity+Router❖ Interactor: Business Logic, using external data access

layer❖ Presenter: UI related (but UIKit independent) business

logic❖ Entities: Slim Model (Plain Data Objects)❖ Router: Routing between different modules

Page 16: iOS Architecture

VIPER: Highlights

❖ Evenly distribute responsibilities❖ UseCase focused❖ Able to combine different UI protocols to build up a screen

Page 17: iOS Architecture

VIPER: Problems

❖ Too many classes: Development/Maintenance Cost ⬆❖ Too complex for simple apps: Slow down the development

Page 18: iOS Architecture

VIPER: Suggestions

❖ Workload Reduce: Use Script/Template to generate all those classes

❖ Not a practical choice for s/m projects❖ A option for large projects (Team Size ≈ 5/Development

Life Cycle ≥ 6 month)

Page 19: iOS Architecture

Ziggurat❖ Created by Square❖ Inspired by React Native & Flux

Page 20: iOS Architecture

Ziggurat❖ More layers distribute

responsibility evenly❖ The service layer

encapsulates mutability❖ Presenter & Renders are

stateless❖ One Way Data Flow❖ Repository = Data Store❖ View Model = View Data❖ Drawback: Relatively

hard to manage animations

Page 21: iOS Architecture

Rules Of Thumb❖ Dependency is the No.1 concern❖ More layers = Less coupling = Harder to

understand❖ No right or wrong architecture❖ Plan well then improve your architecture

iteratively

Page 22: iOS Architecture

Massive Class Prevention

❖ No more than 300 lines of code

Page 23: iOS Architecture

Future Topics

❖ Architecture Design By Practice❖ Protocol Oriented Design in Swift❖ iOS Animation Design By A Live Example/Examples

Page 24: iOS Architecture

References❖ http://casatwy.com/iosying-yong-jia-gou-tan-viewceng-de-zu-zhi-he-diao-yong

-fang-an.html❖ https://www.objc.io/issues/13-architecture/viper/❖ https://www.objc.io/issues/13-architecture/mvvm/❖ https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c3

8de52#.tlnbspmx8❖ https://www.raywenderlich.com/62699/reactivecocoa-tutorial-pt1❖ https://www.youtube.com/watch?v=4cP1p5VOrSI❖ https://corner.squareup.com/2015/12/ziggurat-ios-app-architecture.html❖ http://khanlou.com/2015/12/mvvm-is-not-very-good/❖ http://limboy.me/ios/2015/09/27/ios-mvvm-without-reactivecocoa.html