Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A clean way to approach this is to use a <strong>visitor</strong> pattern to <strong>keep your encoding implementation separate from your business objects</strong>. Some people will argue that you can simply implement the <code>Externalizable</code> interface along with <code>readExternal</code> / <code>writeExternal</code> but this has the problems that:</p> <ul> <li>Your protocol is embededded within your business object, meaning it is distributed across your codebase rather than being in one place.</li> <li>Your application cannot support multiple protocols (as offered by Google Protocol Buffers).</li> </ul> <p><strong>Example</strong></p> <pre><code>/** * Our visitor definition. Includes a visit method for each * object it is capable of encoding. */ public interface Encoder { void visitAnimal(Animal a); void visitVegetable(Vegetable v); void visitMineral(Mineral m); } /** * Interface to be implemented by each class that can be encoded. */ public interface Encodable { void applyEncoder(Encoder e); } public class Animal implements Encodable { public void applyEncoder(Encoder e) { // Make call back to encoder to encode this particular Animal. // Different encoder implementations can be passed to an Animal // *without* it caring. e.visitAnimal(this); } } </code></pre> <p>Typically one would then define a stateful <code>Encoder</code> implementation that would "push" each object to an <code>OutputStream</code> when its <code>visitXXX</code> method is called; e.g.</p> <pre><code>public class EncoderImpl implements Encoder { private final DataOutputStream daos; public EncoderImpl(File file) throws IOException { this.daos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); } public void visitAnimal(Animal a) { daos.writeInt(a.getWeight()); daos.writeUTF(a.getName()); // Write the number of children followed by an encoding of each child animal. // This allows for easy decoding. daos.writeInt(a.getChildren().size()); for (Animal child : a.getChildren()) { visitAnimal(child); } } // TODO: Implement other visitXXX methods. /** * Called after visiting each object that requires serializing. */ public void done() { daos.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload