Hi<div><br></div><div>I want to encode and decode a message which contains a list of Type-Length-Value (TLV).</div><div><br></div><div>So I have a class representing a message, and different classes for the TLVs.</div><div>
For example,</div><div><br></div><div>class Message {</div><div>    List&lt;TLV&gt; typeLenthValues;</div><div>}</div><div><br></div><div>interface TLV {</div><div>    int getType();</div><div>    int getLength();</div><div>
    byte[] getValue();</div><div>}</div><div><br></div><div>class NameTLV implements TLV {</div><div>    void setFirstName() { ... }</div><div>    void setLastName() { ... }</div><div>}</div><div><br></div><div><div>class AddressTLV implements TLV {</div>
<div>    void setPostCode() { ... }</div><div>    void setStreet() { ... }</div><div>}</div><div><br></div><div><br></div><div>The encoding is straight-forward, I&#39;m able to create one generic encoder that handle all the different TLVs.</div>
<div><br></div><div>Unfortunately I&#39;m not able to create a generic decoder to decode the raw bytes into specific TLV.</div><div>For example the following is the code snippet from my decoder,</div><div><br></div><div>class decoder {</div>
<div>    public TLV decode(...) {</div><div>        TLV tlv = null;</div><div>        if (type == 0) {</div><div>            tlv = decodeName();</div><div>        } else if (type == 1) {</div><div>            tlv = decodeAddress();</div>
<div>        } else if (type == ...) {</div><div>        ...</div><div>        } else if (type == 80) {</div><div>            tlv = decodeX();</div><div>        }</div><div>    }</div><div>}</div><div><br></div><div><br></div>
<div>I don&#39;t like the code where I have to code each of the TLV, and there are more TLVs introduced in the future!</div><div><br></div><div>Do you have any idea of how to create a decoder like this?</div></div><div><br>
</div><div><br>-- <br><br>Hez<br>
</div>