ios - 试图了解何时以及何时不使用单例
<p><p>有很多关于单例的信息,什么时候使用它,为什么你不应该使用它等等。所以为了更好地掌握它,也许有人可以用我正在制作的应用程序中的示例来解释它。 </p>
<p>我正在使用 Parse 创建一个带有用户注册的应用。如果我以这种方式使用单例,这是好的还是坏的做法?我在想我将使用我的 <code>User</code> 类在整个应用程序中用户相关操作,也许创建一次 <code>User</code> 类的实例是个好主意:</p>
<pre><code>//User.h
@interface User : NSObject
+ (instancetype)sharedInstance;
- (void)createNewUser:(NSString *)username password:(NSString *)password email:(NSString*)email;
@end
// User.m
#import "User.h"
#import <Parse/Parse.h>
@implementation User
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [ init];
NSLog(@"sharedInstance User.m");
});
return sharedInstance;
}
- (void)createNewUser:(NSString *)username password:(NSString *)password email:(NSString*)email {
// Create a new user
PFUser *newUser = ;
newUser.username = username;
newUser.password = password;
// Additional user information
newUser[@"email"] = email;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Hooray! Let them use the app now.
NSLog(@"Success created user: %@", newUser);
} else {
NSString *errorString = [@"error"];
// Show the errorString somewhere and let the user try again.
NSLog(@"Error: %@", errorString);
}
}];
}
@end
// LoginViewController.m
#pragma mark - IBActions
- (IBAction)loginButtonClicked:(UIButton *)sender
{
[ createNewUser:self.usernameTextField.text
password:self.passwordTextField.text
email:self.emailTextField.text];
}
</code></pre>
<p>或者这样做更好:</p>
<pre><code>// User.h
@interface User : NSObject
- (void)createNewUser:(NSString *)username password:(NSString *)password email:(NSString*)email;
@end
// User.m
@implementation User
- (void)createNewUser:(NSString *)username password:(NSString *)password email:(NSString*)email {
// Create a new user
PFUser *newUser = ;
newUser.username = username;
newUser.password = password;
// Additional user information
newUser[@"email"] = email;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Hooray! Let them use the app now.
NSLog(@"Success created user: %@", newUser);
} else {
NSString *errorString = [@"error"];
// Show the errorString somewhere and let the user try again.
NSLog(@"Error: %@", errorString);
}
}];
}
@end
// LoginViewController.m
- (IBAction)loginButtonClicked:(UIButton *)sender
{
User *newUser = ;
[newUser createNewUser:self.usernameTextField.text
password:self.passwordTextField.text
email:self.emailTextField.text];
}
</code></pre>
<p>另外,如果我误用了任何方式,请直说,感谢您的诚实!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>当你想把某样东西做成单例时,这样想:</p>
<ul>
<li>这个类<strong>真的</strong>每个应用程序一个<strong>永远</strong>吗? (例如,在您的情况下,我可以看到,一段时间后您会希望管理员以您应用程序的某个用户身份登录,并且您最终会得到两个用户对象)</li>
<li>这个类会<strong>真的</strong>永远如此简单,以至于我不会遭受单例测试和<em>全局化</em>问题的困扰吗(单例使单元测试变得困难,并且它们将全局实体添加到您的程序中使得很难跟踪各个应用程序对其所做的更改)</li>
</ul>
<p>如果任一选项适用于您的情况,请执行以下操作:</p>
<ul>
<li>不要让类(class)成为单例</li>
<li>使用依赖注入(inject)容器或注册表模式将您的类绑定(bind)到它,并始终根据容器/注册表解析该类</li>
</ul></p>
<p style="font-size: 20px;">关于ios - 试图了解何时以及何时不使用单例,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/29854821/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/29854821/
</a>
</p>
页:
[1]