Re: Instance Methods vs. static methods in serialization



Responding to Simon...

Currently I started learning ruby, and together with some other articles (f.e. about serialization in .NET), I found that there is a little konflikt between two approches:

Some say that If I design utility methods, like serialization, why not put it into a static method of the class. Then we would have something like:

ClassX.serialize(objectX).

On the other hand, I found some articles about serialization where the people claimed that this is not really beautifull OOD, and such a method should be part of the object, much like Ruby too implements things. So coude would look similar to:

objectX.serialize();

However, beside asthetic issues I am not really shure what advantage brings the latter over the former?

I think it is largely an aesthetic issue. That's because object methods are really class methods in the sense that there is exactly one implementation (or "value" if you will) of the method per class. As you point out in your example, one can view

objectX.serialize()

as semantically equivalent to the special case of

ClassX.serialize(this)

So one would be hard put to come up with advantages or disadvantages that were not purely in the realm of language design (e.g., the infrastructure behind the notion of 'this'). Having said that, though,...


And is it bad design to have the following (out of an Design and an implementation view):

<java_pseudo_code>
Class X {
public static void Serialize(ClassX x) {
// do serialization
}

public static void serialize() {
Serialize(this);
}
}
</java_pseudo_code>

I would not encode this. Essentially this is doing both via indirection. An implementation has to be provided either as a class method or as an instance method. That should be used directly (i.e., have the client directly invoke whichever is implemented). If the language or a library provides a canned implementation of serialization, use that directly. Otherwise pick an approach, implement it, and access it directly.


*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
Pathfinder is hiring: http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH



.



Relevant Pages

  • Re: User-defined classes and serialization
    ... the base class. ... > public static void RestorePlayListState() throws ... > BaseContainer, all it has added was accessor methods to ... from what I've read regarding these serialization ...
    (comp.lang.java)
  • Instance Methods vs. static methods in serialization
    ... Currently I started learning ruby, and together with some other articles (f.e. ... Some say that If I design utility methods, like serialization, why not put it into a static method of the class. ... public static void Serialize{ ...
    (comp.object)
  • Re: Help java.io.NotSerializableException
    ... Netlopa wrote: ... public static void doSave{ ... which will avoid confusion. ... This is not directly relevant to serialization but this is a terrible class name. ...
    (comp.lang.java.programmer)