ios - A Good design in Swift for a model that has many aspects? -
i ask question example of how define model many aspects in swift, when project gets bigger , bigger , 1 model has many aspects. questions pretty long know how ppl design model in big project. comments or thought appreciated.
let's there model called "book" , defined below:
class book { var id: string var title: string var author: string init?(json: [string: anyobject]) { // parse model json } } book has failable initialiser parses properties json sent server. on view controller a, describes more detailed information mode book, properties added model when used on view controller r a:
class book { var id: string var title: string var author: string // required on view controller var price: int var seriersname: string var reviewnumber: int var detaileddescription: string init?(json: [string: anyobject]) { // parse model json } } on view controller b, want show history of book purchase. model needs additional properties below:
class book { var id: string var title: string var author: string // required on view controller 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 model json } } this definition of book lacks flexibility because json passed failabel initialiser must have of properties on vc uses of properties.
solution a:
think simplest solution declaring additional properties optional, think not cool because whenever optional properties used need checked if not nil.
if let seriesname = book.seriesname { self.seriesnamelable.title = seriesname } this kind of optional binding code overflowed on codes assume. implicit optional binding might able used not safe use.
solution b:
solution might define different models inherits book, booka , bookb. if need model has booka , bookb's aspects @ same time?
i guess there no single solution kind of problem know how other ppl define model in big project. (i wonder if have cool solution using "swift specific" features protocol , extension :). appreciate kind of opinions... thank you.
disclaimer: i'm not swift programmer, extrapolations make other languages same features , swift syntax might not 100% accurate
using protocols like:
class ebook: book, hasonlinesource { ... } class otherkindofbook: book, withcollectoreditions, hasbleh, withfoo {...} but must ask yourself:
do need change dinamically?
if case, need go delegation through composition.
are different parts of application using core models differently?
or in other words, there different users of models needing different behavior? in case, extensions useful since allow expose different behaviors same model depending on context. instance, reporting module can send message numberofreaders while selling module ask promotionalcodes. both using same model, interacting different protocols. in case, have different controllers wanting different things, might apply.
using delegates
this follows composition on inheritance principle, after reviewing how delegates work in swift, understood not native implementation still design pattern (a feature request might say), delegation being made hand.
other languages allow make jssonserializablebook bookprotocol, instead of implementing required on bookprotocol can set delegate upon initialization implement such protocol. delegate internal collaborator of jssonserializablebook , messages part of bookprotocol received jssonserializablebook delegated it. or in other words, message passing handled automatically (here kotlin documentatin on delegates if want check how other language implements it).
if want same on swift, must explicitly make message passing delegate. design has several advantages since there no native support message passing becomes verbose implement.
for further reference can check papers on mixins , traits have insight on design decisions behind features.
Comments
Post a Comment