vendredi 5 février 2016

A Good design in Swift for a model that has many aspects?

I would like to ask a question about a good example of how to define a model with many aspects in swift, especially when the project gets bigger and bigger and one model has many aspects. The questions is pretty long but I just would like to know how ppl design a model in a big project. Any comments or thought would be appreciated.

Let's say there is a model called "Book" and it is defined like below:

class Book {
    var id: String
    var title: String
    var author: String
    init?(json: [String: AnyObject]) {
        // parse the model from JSON
    }
}

Book has a failable initialiser that parses properties from JSON sent from the server. On view controller A, it describes the more detailed information about the mode Book, so some properties are added to the model when it is used on view controller r A:

class Book {
    var id: String
    var title: String
    var author: String

    // required on View Controller A
    var price: Int
    var seriersName: String
    var reviewNumber: Int
    var detailedDescription: String

    init?(json: [String: AnyObject]) {
        // parse the model from JSON
    }
}

On another view controller B, we want to show the history of book purchase. So the model needs additional properties as below:

class Book {
    var id: String
    var title: String
    var author: String

    // required on View Controller A
    var price: Int
    var seriersName: String
    var reviewNumber: Int
    var detailedDescription: String

    // required on View Controller B (Not required on VC A)
    var purchasedDate: NSDate
    var expireDate: NSDate

    init?(json: [String: AnyObject]) {
        // parse the model from JSON
    }
}

This definition of Book lacks flexibility because the JSON passed to the failabel initialiser must have all of the properties even on an VC that uses only some of the properties.

Solution A:
I think the simplest solution for this is just declaring those additional properties as optional, but I personally think this is not so cool because whenever those optional properties are used they need to be checked if they are not nil.

if let seriesName = book.seriesName {
    self.seriesNameLable.title = seriesName
}

This kind of optional binding code will be overflowed all over the codes I assume. Implicit optional binding might be able to used but that is not really safe to use.

Solution B:
Another solution might be to define different models that inherits Book, like BookA and BookB. But what if we need a model that has BookA and BookB's aspects at the same time?

I guess there is no single solution for this kind of problem but I would like to know how other ppl define a model in a big project. (I wonder if someone would have a cool solution using some "swift specific" features like protocol and its extension :). I would appreciate any kind of opinions... Thank you.

Aucun commentaire:

Enregistrer un commentaire