swift: immutability and you

23
APPLICATION ARCHITECTURE USING SWIFT LANGUAGE FEATURES

Upload: jens-ravens

Post on 19-Jul-2015

174 views

Category:

Software


1 download

TRANSCRIPT

APPLICATION ARCHITECTUREUSING SWIFT LANGUAGE

FEATURES

SINGLE RESPONSIBILITY PRINCIPLE

COMMUNICATION PATTERNS

OBJC.IO, ISSUE 7

WHY ARCHITECTURE?▸ embrace change▸ know what’s going on▸ minimize bugs

▸ make testing possible

WHAT IS ARCHITECTURE?

MVC

FAT MODEL

MASSIVE VIEW CONTROLLER

DIVIDE AND CONQUER!

SOME IDEAS

TABLE VIEWS▸ Data Source is just a protocol▸ Android: ListAdapter▸ WWDC 2014,

Session 232: Advanced User Interfaces with Collection Views

FASCADES FOR DEPENDENCIESA facade is an object that provides a simplified interface to a larger

body of code, such as a class library

You can use a protocol and class extensions to ensure interface conformance in your objects.

APPLICATION LAYERS▸ View Controller▸ Domain Logic▸ Networking▸ Persistence

HOW DO LAYERS COMMUNICATE?▸ Delegate▸ Event Bus▸ References▸ Protocols

SERVICEA service is a self-contained unit of functionality.

▸ CLLocationManager▸ CMMotionManager

SERVICE class ViewController: UIViewController {

@IBOutlet var tableView: UITableView! let messageService = MessageService.sharedInstance let locationService = LocationService.sharedInstance

}

MODEL CHANGES

MUTABILITY class Message: NSObject { var text = "" var recipient: String? }

var messages: [Message]

▸ there might be a message without a recipient ▸ properties might change at any given time

▸ react to notifications and KVO

IMMUTABILITY struct Message { let text: String let recipient: String }

let message = Message(text: "Hello World!", recipient: "Marc")

▸ properties can’t change▸ structs are value types - methods become side effects free!

▸ value types are thread safe by default

HOW TO CHANGE THE

UNCHANGEABLE?

HOW TO CHANGE THE UNCHANGEABLE?▸ have a service for model operations

▸ callbacks deliver new object instead of updated one func loadMessages(completion:(([Message]?, NSError?)->())?)

▸ attach new objects to NSNotification payload

THANK YOU.