vendredi 11 mars 2016

TLV (type-length-value) validation Design Pattern

I am thinking what will be the best pattern (if possible from the gang of four book) to implement the following validation scenario:

Let say there is an object that is call TLV (Type Length Value). That looks like that:

public class TLV implements Serializable, TLVInterface {

    private Type type;
    private Length length;
    private Value value;   // the buffer may include sub-TLVs
…
getters/setters + other methods
…
}   

This object can include sub objects of the same type i.e. sub-TLV. I.e. the Value field has ByteBuffer that may include sub-TLVs.
Now what needs to be done, is to make sure that only particular sub-TLVs could be a part of an existent TLV. i.e. a check need to be implemented that the main object type can include only particular subtypes i.e. sub-TLVs.

The TLV object is created using the Builder pattern using fluid interface.

This question is more about how to implement proper validation techniques. I was thinking about to create statistical association for example -> where each “Type” (Integer) number has the subType (Integer) number statically typed in a java structure such as List . Then based on a quick search of that List it will be possible to find if the dissected sub-TLVs types match the one that are in the statically pre-typed List. This will work but I was curios if there is a better/smarter validation design pattern / techniques that I can rely on when using TLV's and Java. Maybe there is a pattern that others already used exactly for that purpose.

Another way that I first thought it could work was to rely on Java Interfaces and use them in order to tag the classes as some sort of a tag, and then iterate over the sub TLV's types build them and find out if they implement particular interface. Now this would mean that I would need to manually create at least one class representing each different TLV type that I would support. That is not feasible in my case since those could be many thousands, and to code that will be a nightmare.

Any ideas/ techniques are greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire