处理静态方法时使代码保持在同一级别
这可能有点主观,但我想听听您对我目前情况的意见。我有一个类将用于序列化/反序列化对象。
public class MyClass
{
public static string ToXmlString( MyClass c ) { /*...*/ }
public static MyClass FromXmlString( string xml ) { /*...*/ }
}
我只喜欢这种方法,因为它使两个功能保持在同一水平。但是,我的目标是避免使用静态方法(如果可行)。我也觉得我可能违反了 SRP,但这个对象的主要目标是它可以从 xml 字符串序列化/反序列化。
对于在这种情况下使用静态方法有什么想法吗?我应该将 ToXmlString
设置为非静态,但将 FromXmlString
保留为静态吗?我应该创建一个仅处理 MyClass 序列化的新类吗?
编辑:
我在这里讨论的类是一个简单的传输对象。它用于从第三方工具保存/恢复值。
谢谢!
This might be a little subjective, but I'd like to get your input on my current situation. I have a class that will be used to serialize/deserialize an object.
public class MyClass
{
public static string ToXmlString( MyClass c ) { /*...*/ }
public static MyClass FromXmlString( string xml ) { /*...*/ }
}
I only like this approach because it keeps the two functions at the same level. However, my goal is to avoid using static methods (when feasable). It also feels like I might be vilolating SRP, but the main goal of this object is that it can be seriliazed/deserialized from an xml string.
Any thoughts on the use of static methods in this situation? Should I just make the ToXmlString
non-static, but leave the FromXmlString
static? Should I create a new class that will only handle serilization of MyClass?
EDIT:
The class that I'm discussion here is a simple transfer object. It is used to save/restore values from a thrid party tool.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
FWIW 我认为序列化是一个问题,应该与班级的其他部分分开,尤其是如果您的班级是业务类型。
开发组件时的一般规则是确保它只解决少数问题,并将业务问题与技术问题分开。
如果以后您需要管理数据库或二进制格式的序列化怎么办?
您可能会以越来越多的技术方法(SaveToDB、LoadFromDB、ToBinaryStream、FromBinaryStream...)结束,这些方法会使您的类变得混乱,使其变得越来越难以维护,隐藏其主要用途(例如业务)。
FWIW I think that serialization is a problematic that should be separated from the rest of your class, above all if your class is a business type.
The general rule when developing a component is to ensure that it only addresses a few concerns and to separate business concerns from technical ones.
What if later you need to manage serialization from a database or a binary format ?
You might end with more and more technical methods (SaveToDB, LoadFromDB, ToBinaryStream, FromBinaryStream...) that would clutter your class and make it more and more difficult to maintain, hiding its primary purposes (business for example).
C# 和 Java 的标准库中的约定是,
To__
方法是实例方法,而From__
方法是静态的(必然)。例如:ToString()
是一个实例方法。The convention in the standard libs for both C# and Java is that
To__
methods are instance methods andFrom__
methods are static (by necessity). For example:ToString()
is an instance method.详细说明Benoit的答案,这里有一个示例,其中正在序列化的类定义了序列化行为(我没有写这个):
请注意使用瞬态来描述不会被序列化的字段。
Elaborating on Benoit's answer, here's an example where the class that is being serialized defines the serializing behavior (I did not write this):
Note the use of transient to describes fields that will not be serialized.
如果您想要标准序列化(无论是否为 XML),则序列化/反序列化方法都不应该是静态的。
在MyClass中,您应该重新定义“writeObject”和“readObject”以替换您的默认序列化方法。这是关于这些方法的 Sun 教程。
如果您不想要“标准序列化”,那么使用静态方法对我来说看起来不错。静态 util 方法并不是异端邪说。
PS:这不是问题,但如果您想要 WML 序列化,您可以使用 XStream API。
If you want a standard serialization (XML or not), both serialize/deserialize methods should not be static.
In MyClass, you should redefine "writeObject" and "readObject" to replace the default serialization methods by yours. Here is a Sun tutorial about theses methods.
If you don't want a "standard serialization", using static methods looks fine for me. Static util methods are not an heresy.
PS : it is not the question, but if you want WML serialization, you can use the XStream API.
您可以定义一个采用 XMLReader(如果您确实坚持的话,也可以采用字符串)的构造函数。这样做的主要优点是,它允许您在类中拥有更强的不变量,并通过使用
readonly
明确任何不可变成员。You could define a constructor that takes an XMLReader (or a string if you really insist). The main advantage of this is that it allows you to have stronger invariants in your class, and to be explicit about any immutable members through the use of
readonly
.我认为将静态方法与实例方法分开并不是太糟糕,因为框架偶尔会这样做(例如 String.Split / Join)。
但话虽如此,我认为最小化静态方法的使用的目标并不是一个好主意。要避免的是静态可变状态,而不是静态方法。仅对其参数而不是静态变量进行操作的静态方法真是太棒了。
纯静态函数比实例方法更易于维护,因为实例方法不会以明显的方式传达它可以改变哪些实例字段。通过遵循不维护任何静态的规则,静态方法可以仅对其参数进行操作,因此可以更好地预测该方法在应用程序中的作用。这在多线程时尤其重要。
由于 ToXmlString 方法应用于定义该方法的类的实例,因此其中一些注意事项不适用。它可以轻松地以不正当的方式更改传递给它的对象的状态,因为它可以访问实例的所有私有成员。但我只是想说,作为一般规则,静态方法不是问题。
I don't think it's too terrible for complementary methods to be separated with regard to static vs. instance, since the Framework does this occasionally (String.Split / Join, for example).
But having said that, I think the goal of minimizing the use of static methods is not a good idea. The thing to avoid is static mutable state, not static methods. A static method that only operates on its parameters, rather than static variables, is pure awesomeness.
A pure static function can be more maintainable than an instance method, since the instance method does not communicate in an obvious way which instance fields it can mutate. By following the rule that no static state whatsoever is maintained, a static method can be relied upon to only operate on its parameters, and thus the method's role in the application can be better predicted. This is especially important when multi-threading.
Since the ToXmlString method is being applied to an instance of the class in which it is defined, some of these considerations don't apply. It could easily change the state of the object that is being passed to it in underhanded ways, since it can access all the private members of the instance. But I just mean to say that as a general rule static methods are not a problem.