mardi 27 février 2018

iOS Design Pattern for Setting Page

Recently, I am going to create a setting page for my app. My design is using one singleton object to link up with different setting object, like language setting, notification setting, etc., Here is my code:

class AppSettings{

  static let settings = AppSettings()
  let langSetting
  let notificationSetting

  private init(){
    langSetting = new LangSetting()
    notificationSetting = new NotificationSetting()
  }
  public getSettings() -> AppSettings{
     return settings
  }
}

However, the above code violated "open for extend, close for modification" principle. And also violated "depend on abstractions not on concrete implementations". But how to modify it to fulfil those principle?

Another problem is that, if I want to change the language, I need to write some code like this:

AppSettings.getSettings().langSetting.set("en")

Is it to long for the others to use this API?

Aucun commentaire:

Enregistrer un commentaire