Re: Instance Methods vs. static methods in serialization
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Tue, 30 May 2006 21:36:05 GMT
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
.
- References:
- Instance Methods vs. static methods in serialization
- From: Joerg Simon
- Instance Methods vs. static methods in serialization
- Prev by Date: Re: sequence diagram for observer pattern
- Next by Date: Re: Searching OO Associations with RDBMS Persistence Models
- Previous by thread: Instance Methods vs. static methods in serialization
- Next by thread: Re: The wisdom of the object mentors (Was: Searching OO Associations with RDBMS Persistence Models)
- Index(es):
Relevant Pages
|