18

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!

barbarity
  • 2,420
  • 1
  • 21
  • 29

2 Answers2

17

A few suggestions

  1. Design patterns are a guide to help save you effort from resolving already solved problems, they aren't strict rules
  2. While the site you link to (raywenderlich.com) is a good start for tutorials, for a more detailed look at design patterns in swift I suggest Design Patterns In Swift
  3. If HttpClient and PersistencyManager are base classes which provide the interface than a protocol is not strictly essential. I agree that protocols are a more general way to go here
  4. If you go with protocols, I'd specify the client and persistency manager in the initializer as they are essential
  5. Persisting models is a specific enough role as to be handled by a single class, see realm.io for an example db
Mark Essel
  • 4,606
  • 2
  • 29
  • 47
7

Since the first line of question states that you want to "develop a real application on your own" therefore I just want to point you in the right direction.

The fact is there is no "best" way of structuring your code. There are many ways in which you can write code which accomplishes the same task. And unless you are working in a team and building a very complicated app, it does not really matter which approach you follow.

As you said that you have learned swift, I would suggest you now focus on learning advanced features of swift like closures and protocols. Once you are familiar with these then you would need to get familiar with iOS SDK which has loads of built in frameworks that you would need to use in your app.

Lastly, just get started with your app as soon as possible. You may not be able to write clean and well structured code in your first app but it is something that you will learn over time by making mistakes and correcting them later. Just to encourage you, I would like to tell you that making a simple app is not very tough, I learned objective c and made my first game in 20 days and you can do it too. If you need any tutorials/resources, just leave a comment, I will update the answer.

Focus on building the app. Improve it later when you have built it.

Ankit Goel
  • 6,257
  • 4
  • 36
  • 48
  • One of my persistent mistakes. Always wanted to do it perfectly since the beginning. But I will follow your advice. Thank you! – barbarity May 28 '15 at 10:34
  • 1
    If you want to start your own app until you master the best coding design and patterns and so on... you'll never start your app. That was my problem by the way. I was thinking about every detail of the app, while the app didn't exist at all. Just start and learn from your mistakes. I can tell my son to watch out for all dangerous things in live, but he will actually learn it if he falls, cause than he can learn to stand up. Just do it, we'll deal with the problem later :) – AndaluZ Oct 06 '16 at 12:38