0

In my github repo I have hierarchy of classes and of course serialization/deserialization mechanism is present for them.

I serializing them manually via Externalizable and I want to take all of the code that generates values, needed for instance serialization out of this classes to keep all simple and flexible (or just to take out this mess)

So what I basically want to do is to create SerializationHelpers and DeserializationHelpers classes, where the name of particular class will be NameOfClassSerializationHelper.

Names like this are 29 characters in worst case, but I think that this is too much. Yeah of course it provides better understanding of what's going on and the name is less then 50 chars and user will never see this classes.

Here is scatch of interface hierarchy for those helper classes. interfac hierarchy

So as you can see I reduced Serialization to Ser and Deserialization to Deser but seems like it hurts readabuility.

For example class that implements TrieSerializationHelper will have name LinkedTrieSerializationHelper.

There is one another trouble: I can't place those serialization/deserialization helpers to another package because they use some package-private class (Node, as you can see from restoreRooot method of WordGraphDeserHelper).

So I'm totally confused how to do better and exactly what I have to do. Thanks in advance.

Mike Herasimov
  • 1,319
  • 3
  • 14
  • 31

1 Answers1

1

You may also want to create something like Externalizer interface with specific implementations (TrieExternalizer) and move all logic there so your main classes (I think, they are Trie and DAWG) won't be overloaded with serialization/deserialization.

Example:

public interface Externalizer<T> {
    void write(T object, ObjectOutput out);
    void read(T object, ObjectInput in);
}

class TrieExternalizer implements Externalizer<Trie> {
    public void write(Trie object, ObjectOutput out) throws IOException {
        out.writeUTF(object.getSomeField());
    }


    public void read(Trie object, ObjectInput in) throws IOException {
        object.setSomeField(in.readUTF());
    }
}

class Trie implements Externalizable {
    private String someField;
    private static final Externalizer<Trie> externalizer = new TrieExternalizer();

    public String getSomeField() {
        return someField;
    }

    public void setSomeField(String someField) {
        this.someField = someField;
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        externalizer.write(this, out);
    }

    public void readExternal(ObjectInput in) throws IOException {
        externalizer.read(this, in);
    }
}
ovnia
  • 2,412
  • 4
  • 33
  • 54
  • 1
    sorry for late response, and yeah, your idea is MUCH better then mine, you putting exactly whole logic of serialization/deserialization to descendants of `Externalizer` interface (by call readressing) and it reduces number of classes in packages – Mike Herasimov May 31 '16 at 08:22