mercredi 16 novembre 2016

IOS Swift Networking with Flexible Backend Design Pattern

I am trying to architect an app that can flexibly swap out the backend web service with minimal changes to the codes.

I'm new to design patterns and read about the SOLID principles that codes should be closed to modifications but opened to extensions.

Below is what I came up with in pseudo code. Please let me know what you think of my codes and if there is a better way to approach this, or rules I should be following. Thanks!

class User {
    var name = ""
    var email = ""

    init(name:String, email: String){
        self.name = name
        self.email = email
    }
}
class Comment {
    var comment = ""
    var user = User(name: "", email: "")

    init(user: User, comment: String){
        self.user  = user
        self.comment = comment
    }
}
enum RemoteStore: String{
    case Parse = "http://ift.tt/2giqZrW"
    case Firebase = "http://ift.tt/2fWtgFH"
    case RESTfulServer = "http://ift.tt/2girwu9"
}
enum HTTPMethod: String {
    case GET = "GET"
    case POST = "POST"
}
var comment = Comment(user: User(name: "John Doe", email: "johndoe@gmail.com"), comment: "Hello World")
// Example of GET Method with Parse
Alamofire.request(HTTPMethod.GET, RemoteStore.Parse)
    .response { request, response, data, error in
        print(request)
        print(response)
        print(error)
}
// Example of POST Method with Firebase
Alamofire.request(HTTPMethod.POST, RemoteStore.Firebase, parameters: [comment])
    .response { request, response, data, error in
        print(request)
        print(response)
        print(error)
}
// Example of GET Method with RESTFUL server
Alamofire.request(HTTPMethod.GET, RemoteStore.RESTfulServer)
    .response { request, response, data, error in
        print(request)
        print(response)
        print(error)
}

Aucun commentaire:

Enregistrer un commentaire