我不知道我该用什么。我有3个班。 PasswordService
,SettingsService
,FileService
。这些类每个包含大约2种方法。这些方法正在用于更多的程序集。现在我将它用作单例。但我不确定是否应该。我认为一个静态类就足够了。 你觉得呢?单身或静态 - 我应该使用什么?
CODE:
public class PasswordService
{
private PasswordService(){}
private static PasswordService _instance;
public static PasswordService Instance
{
get { return _instance ?? (_instance = new PasswordService()); }
}
public byte[] EncryptPassword(string password)
{
var protectedPass = Encoding.UTF8.GetBytes(password);
return ProtectedData.Protect(protectedPass, null);
}
public string DecryptPassword(byte[] encryptedPassword)
{
var unprotectedPass = ProtectedData.Unprotect(encryptedPassword, null);
return Encoding.UTF8.GetString(unprotectedPass, 0, unprotectedPass.Length);
}
}
该问题的答案总是相同的:这取决于。 – Nuffin 2013-03-18 11:15:03
可能重复的[我应该使用单身?](http://stackoverflow.com/questions/1676296/should-i-use-a-singleton) – 2013-03-18 11:16:17
我会说单身几乎总是邪恶的。使用这些服务中的每一个的实例。最终你会得到一个更灵活的设计,你可以看到你真正的依赖。 – 2013-03-18 11:16:35