Recently, I've learned Swift and the basics to develop an iOS app. Now, I want to develop a real application by my own, but I'm very concerned about writing good code, so I've looked up for "best practices", "design patterns" and "the right way" to achieve it.
On my search, I've found this great tutorial about all the design patterns normally used in a Swift iOS app and examples of where are they used.
But nevertheless I consider this tutorial a great one and helped me a lot, I have the feeling that it's only a start, because I see many S.O.L.I.D. principles violations. For example:
See the Façade Pattern implemented in LibraryAPI:
class LibraryAPI: NSObject {
private let persistencyManager: PersistencyManager
private let httpClient: HTTPClient
private let isOnline: Bool
class var sharedInstance: LibraryAPI {
struct Singleton {
static let instance = LibraryAPI()
}
return Singleton.instance
}
override init() {
persistencyManager = PersistencyManager()
httpClient = HTTPClient()
isOnline = false
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"downloadImage:", name: "BLDownloadImageNotification", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func getAlbums() -> [Album] {
// ... Not relevant
}
func addAlbum(album: Album, index: Int) {
// ... Not relevant
}
func deleteAlbum(index: Int) {
// ... Not relevant
}
func downloadImage(notification: NSNotification) {
// ... Not relevant
}
}
The first thing that comes to my mind seeing this is: Doesn't this violate the Depedency Inversion Principle? Shouldn't be httpClient
and persistencyManager
be declared as protocols and then the classes HttpClient
and PersistencyManager
implement that protocol?
If that's the case, at some point, I will have to define what classes, which implements those protocols, I'm going to use. Where should I tell the app?
Another question I have is: This example only implements one Model (Album
), but what if it would implement many others? (Album
, Author
, Genre
...). Wouldn't be LibraryAPI
be so big that it would violate the Single Responsability Principle?
And last but not least... Same problem with the DIP exist in PersistencyManager
. Shouldn't it implement DAO pattern, so `PersistencyManager does not depend on the other classes?
Thank you in advance, and I hope I explained myself good enough!
Swift is the coding language you'll need to learn for writing iOS apps. After downloading Xcode, you will be writing Swift into Xcode.
Results. According to this analysis, of the top 110 apps on the app store on January 15, 2019, 42% are using Swift, while 58% are not. If only the 79 non-game apps are considered, the results are that 57% are using Swift, while 43% are not.
A few suggestions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With