I have been developing Cocoa Apps for a while and I have a conceptual question regarding the Singleton "pattern" and the use of the NSNotificationCenter
for communication.
Suppose I have a class that is responsible for storing the credentials of the user in the app. Lets call it UserAccountController
. Such class exposes public methods to perform login/logout operations and notify any interested object that such operations were performed (e.g.: in a tab bar application,开发者_开发技巧 I'd like to update all UIViiewControllers when the user logged out).
In my opinion, it wouldn't make sense to have more than one UserAccountController
object in the application, also, a second UserAccountController
object could also post notifications to the NSNotificationCenter
, which may cause troubles to objects registered to receive such notifications.
Given this situation I have two questions:
- What pattern to use in classes like
UserAccountController
. - Any class that uses NSNotifications for information flow in the application should, necessarily, implement the Singleton "pattern"?
By analyzing the Apple's classes I have found that the question 2) makes sense, but I would like to avoid the Singleton "pattern".
Any clues?
I would store the user credentials in UserAccountModel
object. That object would hold among other things my current state, i.e. login status. Whenever that status changes, it would post notification to that fact. My UIApplicationDelegate
would hold a reference to that model. Now, let's say I have a view where the user enters login credentials, LoginView
. My MVC would be UserAccountModel
-> LoginViewController
-> LoginView
.
精彩评论