开发者

What are the key points to make sure while implementing serialization

开发者 https://www.devze.com 2023-02-14 00:41 出处:网络
What are the key points in checklist to be checked while implementing good serialization in java (I\'m not really looking开发者_如何学编程 for , implements Serializable, write, readObject etc).

What are the key points in checklist to be checked while implementing good serialization in java (I'm not really looking开发者_如何学编程 for , implements Serializable, write, readObject etc).

Instead , How to reduce the size of the object , Probably how to make the object in zip format and send over the network etc.. How to ensure the secure mode of transfer. any others like this..


How to reduce the size of the object: new ObjectOutputStream(new GZipOutputStream(new BufferedOutputStream(out)). But this is a space-time tradeoff. You may find that it makes performance worse by adding latency.

How to ensure the secure mode of transfer: SSLSocket or an HTTPS URL.

Any others like this

Any others like what? You will need to be specific.


  • Do not use serialization to "persist" objects - this makes schema management (i.e. changing what constitutes a class's state) almost unworkable

  • Always declare a serialVersionUID field; otherwise you will not be able to add methods, or change the class in any way (even non-state-changing ones) without old versions of your code being unable to deserialize the objects (an IncompatibleClassVersionError will be thrown)

  • try and override readResolve if you are deserializing a logical "enum" instance of a class (typesafe enumeration pattern)

  • Make sure you are 100% happy with the name of the variables which make up the state of your class. Once you have serialized instances around, you cannot change the variable names

  • Do not implement Serializable unless you really have to

  • Do not make your interfaces Serializable - the implementation classes may be, but the interfaces should not be

  • Do not make serialization part of the way your library passes objects around, unless you are the only producer and consumer of the objects (e.g. server-GUI communication). Use a binary/wire protocol instead (e.g. protobuf)

  • To minimize what is sent over the wire, you could use swizzling. That is, perhaps you have a Product class; the serialized form might just be a unique int id field. All other methods could then be made to construct relevant state as required (perhaps as a database call, or call to some central service)

  • Make sure, if you are serializing out an object which contains some collection of elements as part of its state, that you synchronize on the collection. Otherwise you may find that someone modifies the collection as it is being serialized out, resulting in a ConcurrentModificationException


Probably how to make the object in zip format and send over the network etc..

Check out how the game developers for network games implement networking. They know, how to transmit data quickly. Have a look at e.g. http://code.google.com/p/kryonet/

How to ensure the secure mode of transfer. any others like this..

There are a lot of interpretations of secure mode. If you need reliability, use TCP otherwise UDP. If you need encryption use TLS otherwise rot13 may fit. If you need to ensure integrity, append a hash of the values to the message.

How to reduce the size of the object ,

Analyse your data and strip down the objects, so that you only have the necessary data in there. This is very context specific, as the best optimisation can be in the domain. E.g. you can check, if it is possible to send only deltas of the change.

It is an interesting question, but you have to be more specific about your goal or domain to get an answer that fits best.

0

精彩评论

暂无评论...
验证码 换一张
取 消