静态类设计?
我想知道这是否是一个好的做法,我不确定这个类是否应该是静态类?
public class SettingsHelper
{
public static readonly string MinVal= "MinVal";
public static readonly string MinPartners = "MinPartners";
public static IDictionary<string, string> GetSettings(string jsonsettings)
{
var settings = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonsettings);
return settings;
}
public string SettingsToJson(IDictionary<string, string> settings)
{
var jsonsettings = JsonConvert.SerializeObject(settings);
return jsonsettings;
}
public static decimal GetMinPartners(string jsonsettings)
{
var settings = GetSettings(jsonsettings);
string partners;
settings.TryGetValue(MinPartners, out partners);
return decimal.Parse(partners);
}
public static int GetMinValue(string jsonsettings)
{
var settings = GetSettings(jsonsettings);
string pival;
settings.TryGetValue(MinVal, out pival);
return int.Parse(pival);
}
}
我想包括更新合作伙伴、添加合作伙伴等方法......
I wanted to know if this was good practice or not, I am not really sure if this class should be static class or not?
public class SettingsHelper
{
public static readonly string MinVal= "MinVal";
public static readonly string MinPartners = "MinPartners";
public static IDictionary<string, string> GetSettings(string jsonsettings)
{
var settings = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonsettings);
return settings;
}
public string SettingsToJson(IDictionary<string, string> settings)
{
var jsonsettings = JsonConvert.SerializeObject(settings);
return jsonsettings;
}
public static decimal GetMinPartners(string jsonsettings)
{
var settings = GetSettings(jsonsettings);
string partners;
settings.TryGetValue(MinPartners, out partners);
return decimal.Parse(partners);
}
public static int GetMinValue(string jsonsettings)
{
var settings = GetSettings(jsonsettings);
string pival;
settings.TryGetValue(MinVal, out pival);
return int.Parse(pival);
}
}
I want to include methods such as, update partners, add partners etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为将其静态化有什么好处?它可以很好地在需要的地方实例化它。
不过,我会将其命名为
ConfigurationRepository
。它更好地描述了意图。此外,我会将序列化移至第二类,以保持责任清晰。I don't see any benefit in having it static? It works just fine to instantiate it where ever it's needed.
I would name it
ConfigurationRepository
though. It describes the intent better. Also I would move the serialization to a second class to keep the responsibilites clear.我建议尽可能避免静电。
- 它使你的代码更难测试。您无法在测试用例中切换到不同版本。同样,mockito 等测试框架不能很好地支持静态。
,则静态类才有意义
如果您的所有方法都是静态的
-你在类中不持有任何全局状态。
-您不修改任何成员变量。
-你所有的功能本质上都是“帮手”。
-您的类没有创建其他静态类的链。
至于线程安全,由于您的类没有修改任何全局状态(任何成员变量),因此它是线程安全的。
I would advise to avoid statics whenever possible.
- It makes your code less testable. You can not switch to a different version in your test cases. Again testing frameworks such as mockito do not support static well.
Static class makes sense if,
-all your methods are static
-you do not hold any global state in the class.
-you do not modify any of the member variables.
-all your functions are "helper" in nature.
-Your class is not creating a chain of other static classes.
As for Thread-safety, since your class is not modifying any global state (any member variable), it is thread-safe.
我认为“静态类”是指仅具有静态方法的类。
使用静态方法的充分理由很少。也许如果你需要实现一个真正的单例(如果它存在两次就会试图杀死你)。但这里的情况似乎并非如此。
静态方法有一些可接受的原因,例如
这些可能适用于您的情况。 =>所以设计可能没问题。
也许一个合适的类(或者可能每个方法一个类)可能会更好。
方法/类的客户端将在构造函数中获得一个实例,可能使用创建普通实例的默认构造函数。
以下是您将获得的结果:
在测试客户端时,您可以轻松地通过模拟提供预设答案,无需使用有效的 json 字符串。如果客户端本身不创建这些字符串,那么当 json 字符串的结构发生变化时,这可以防止与测试中的实际类无关的大量失败。
I assume that with 'static class' you mean a class with only static methods.
There are very few good reasons for static methods. Maybe if you need to implement a real Singleton (something that will try to kill you if it exists twice). But this doesn't seem to be the case here.
There are some acceptable reasons for static methods like
These might apply in your case. => so the design might be ok.
Probably a proper class (or possibly a class per method) might be even better.
Clients of your methods/classes would get an instance in the constructor, possibly with a default constructor which creates the normal instances.
Here is what you would gain:
When testing clients you can easily provide canned answers with mocks, you don't need to use valid json strings. In case of clients that don't create these Strings themselves this prevents tons of failures unrelated to the actual class under test when the structure of your json string changes.
When some day some of this information comes from xml, a database or divination, especially when this distinction is made by client and not be type of information, it will be easy to switch.
它必须是一个静态类,更不用说如果类中的所有函数都是静态的......
类中是否存在依赖于对象标识的数据或行为?
it need to be a static class, much less if all your functions in the class are static...
there is a data or behavior in the class that depends on object identity?